request
1.1、request简介
是什么 ?:a simplified HTTP request client.
开发语言:JavaScript
源码仓库:https://github.com/request/request

注意:该包已经被标记为Deprecated,请使用其他包代替之,比如axios

1.2、通过包管理器安装request
包管理器安装命令
npmnpm install request --save
yarnyarn add request
1.3、导入request模块
var request = require("request");
1.4、request(String uri [, callback])

GET请求。

uri是要请求的地址。

callback是请求完成的回掉函数。函数原型是function (error, response, body)

示例:

var request = require('request');
request('http://www.baidu.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the baidu homepage.
  }
});
1.5、request(Object options [, callback])

可以是任意请求。

callback是请求完成的回掉函数。函数原型是function (error, response, body)

options对象中有下面这些字段:

字段类型说明
uriString要请求的地址
methodString请求类型,比如GET、POST、PUT、DELETE、HEAD等
headersObject设置请求头
bodyString请求体内容
jsonBoolean请求题是否是JSON格式
gzipBoolean是否支持gzip压缩,默认是false
jarBoolean是否支持Cookies,默认是false
formObject
会把form对象自动转成form格式,并经过URL编码。
会自动添加Content-Type: x-www-form-urlencoded请求头。
formDataObject
会把formData对象自动转成formData格式。
会自动添加Content-Type: multipart/form-data请求头。
encodingString字符编码,默认是utf-8,如果是二进制数据,请设置为null
timeoutInteger超时,单位是s
1.5.1、POST application/json
request({
    url: url,
    method: "POST",
    json: true,
    headers: {
        "content-type": "application/json",
    },
    body: JSON.stringify({xx: 'xxx', yy: 'yyy'})
}, function(error, response, body) {
    if (!error && response.statusCode == 200) {
        // TODO
    }
});
1.5.2、POST x-www-form-urlencoded
request({
    url: url,
    method: "POST",
    form: {xx: 'xxx', yy: 'yyy'}
}, function(error, response, body) {
    if (!error && response.statusCode == 200) {
        // TODO
    }
});
1.5.3、POST multipart/form-data
request({
    url: url,
    method: "POST",
    formData: {
        // Pass a simple key-value pair
        my_field: 'my_value',
        // Pass data via Buffers
        my_buffer: new Buffer([1, 2, 3]),
        // Pass data via Streams
        my_file: fs.createReadStream(__dirname + '/unicycle.jpg')
    }
}, function(error, response, body) {
    if (!error && response.statusCode == 200) {
        // TODO
    }
});
1.6、request.get(Object options [, callback])

request(Object options [, callback])相同。默认是GET请求。

1.7、request.post(Object options [, callback])

request(Object options [, callback])唯一的区别是,此方法默认是POST请求。

1.8、request.put(Object options [, callback])

request(Object options [, callback])唯一的区别是,此方法默认是PUT请求。

1.9、request.delete(Object options [, callback])

request(Object options [, callback])唯一的区别是,此方法默认是DELETE请求。

1.10、request.head(Object options [, callback])

request(Object options [, callback])唯一的区别是,此方法默认是HEAD请求。

1.11、request.patch(Object options [, callback])

request(Object options [, callback])唯一的区别是,此方法默认是PATCH请求。

1.12、request.pipe(destination [,Object options])

管道,这个用起来和Shell里面的管道很像。

示例1:

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'));

示例2:

fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'));
1.13、request.on(String event, Function callback)
1.13.1、response

当获得请求结果时候触发。

示例:

request('http://google.com/img.png')
    .on('response', function(response) {
        console.log(response.statusCode);
        console.log(response.headers['content-type']);
    })
    .pipe(request.put('http://mysite.com/img.png'));
1.13.2、error

当出错时候触发。

示例:

request('http://mysite.com/doodle.png')
    .on('error', function(err) {
        console.log(err);
    })
    .pipe(fs.createWriteStream('doodle.png'));