【nodejs】request使用文档

使用request在nodejs程序中进行网络请求文档介绍。

【nodejs】request使用文档

By img Microanswer Create at:Dec 10, 2019, 9:07:00 AM 

Tags: request 网络请求 http

使用request在nodejs程序中进行网络请求文档介绍。


requestaxios一样,可以作为网络请求工具。它们的区别就是request只适用于nodejs后台使用,不能使用在前端网页上,而axios实现网络请求时同时兼容了nodejs后台和网页前端两种,并且会根据运行环境自动使用对应的请求方式。所以axios可以用于前端请求,而request则仅适用于nodejs后端程序。

一、安装

对于安装,不必多说,直接使用命令:

npm install request --save

完成安装,并将依赖写入package.json文件里。

二、使用

值得注意,request会自动跟踪重定向,这点和axios相同,这极大地方便了爬虫相关的开发。但现在先看看基本用法如何。

1、GET请求

var request = require('request');
request('https://www.microanswer.com', function (error, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.
});

或者这样

var request = require('request');
request.get('https://www.microanswer.com', function (error, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.
});

2、POST请求

var request = require('request');
request.post("https://www.microanswer.cn", function (err, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.
});
携带参数 - application/x-www-form-urlencoded
var request = require('request');
request.post("https://www.microanswer.cn", {
    form: {
        name: "Jack",
        age: 11
    }
}, function (err, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.
});
携带参数 - application/json
var request = require('request');
request.post("https://www.microanswer.cn", {
    body: {
        name: "Jack",
        age: 11
    },
    json: true // 上面body传递了一个json对象,此处json必须为true, 如果此处json未设置,则body需传递字符串,就像这样 body: JSON.stringify({name:"Jack",age:11});
}, function (err, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.当你设置了json:true 时,如果服务端返回的是json字符串,则这里可直接把body当做json对象使用。request已自动帮你转成对象。
});

3、上传文件

var request = require('request');
let fs = require("fs");

request.post("https://www.****ttt.cn/uploadTest", {
    formData: {
        "name": "Jack",// 这就像表单里一个input输入了一个文本内容
        "file": fs.createReadStream("D:/text.txt"), // 这就像表单里一个input的type类型为file,选了文件text.txt
        "file2": Buffer.from("****") // 还可以提交任意的二进制内容
    }
}, function (err, response, body) {
    // error 错误信息.
    // response 响应.
    // body 这是响应体数据内容.
});

文件上传使用的form-data规则是来自 @felixgeform-data 项目,GITHUB地址https://github.com/form-data/form-data。要查看更多文件上传的细节,可以到此地址查看。

4、文件下载

request下载文件非常方便。如下:

var request = require('request');
let fs = require("fs");

// 下载图片到指定路径。
request.get('http://google.com/img.png').pipe(fs.createWriteStream("D:/text.png"));

三、配置

除了类似这样的请求:

request.get("https://www.microanswer.cn/", function (err, res, body) {/**/});
request.post("https://www.microanswer.cn/", function (err, res, body) {/**/});

还可以这样请求:

request.get({url:"https://www.microanswer.cn/"}, function (err, res, body) {/**/});
request.post({url:"https://www.microanswer.cn/"}, function (err, res, body) {/**/});

也就是第一个参数允许直接是一个配置对象,此对象只有 uri 是必填项,其余都是选填项。有关这个配置对象的所有配置如下:

{
    uri: [String], // 要请求的路径
    url: [Object], // 要请求的路径的url对象,此对象来自:url.parse(uri)。 这俩参数必填一个。
    baseUrl: [String], // 基础路径,使用了这个之后,uri就可以只传递基础路径后半部分
    method: [String], // 请求方法
    headers: [Object], // 请求头,默认是{},没有任何请求头。
    timeout: [Number], // 毫秒单位。超时时间
    body: [Buffer,String,ReadStream,Object], // 当请求方式为 PATCH, POST 和 PUT 时,此字段可指定要提交的请求体。 只有当 json 设置为 true 时, body 才可以是一个对象,此对象必须是可序列化为json字符串的。
    form: [String,Object], // application/x-www-form-urlencoded 表单请求,当传递字符串时,格式务必满足urlencoded的规范。
    formData: [Object], // 上传文件时使用。
    encoding: [String], // 默认utf8,如果传递null,则返回的数据变成Buffer,你需要自己处理这个数据为你需要的数据。
    gzip: [Boolean], // 如果响应时gzip的,是否进行自动解压,我在使用这个时各种问题,就没用它。而是使用encoding为null,然后自己使用zlib框架进行的解压。
    jar: [Boolean, Object], // 如果设定为true, 则在此请求的后续请求带上cookie,如果此请求会返回cookie的话。这对爬虫非常有用。还可以指定自定义的cookiejar处理器,下文有介绍
    agent: [String], // 就像浏览器的agent一样,你可以指定一个你喜欢的。

    qs: [Object], // 设置使用参数序列化的工具,用于把参数放在uri上。
    qsParseOptions: [Object], // qs工具在parse时使用的配置
    qsStringifyOptions: [Object], // qs工具在Stringify是使用的配置
    useQuerystring: [Boolean], // if true, use querystring to stringify and parse querystrings, otherwise use qs (default: false). Set this option to true if you need arrays to be serialized as foo=bar&foo=baz instead of the default foo[0]=bar&foo[1]=baz.
    multipart: [Array], // array of objects which contain their own headers and body attributes. Sends a multipart/related request.
    preambleCRLF: [String], // 较底层,表单提交时换行前置符
    postambleCRLF: [String], // 较底层,表单提交时换行后置符
    json: [Boolean], // 设置为true时,body字段允许传递一个支持序列化为json字符串的对象,同时返回数据如果为json会自动把返回的数据进行JSON.parse().
    jsonReviver: [Function], // a reviver function that will be passed to JSON.parse() when parsing a JSON response body.
    jsonReplacer: [Function], // a replacer function that will be passed to JSON.stringify() when stringifying a JSON request body.
    auth: [String], // a hash containing values user || username, pass || password, and sendImmediately (optional).
    oauth: [Object], // options for OAuth HMAC-SHA1 signing.
    hawk: [Object], // options for Hawk signing.
    aws: [Object], // object containing AWS signing information. Should have the properties key, secret, and optionally session (note that this only works for services that require session as part of the canonical string). Also requires the property bucket, unless you’re specifying your bucket as part of the path, or the request doesn’t use a bucket (i.e. GET Services). If you want to use AWS sign version 4 use the parameter sign_version with value 4 otherwise the default is version 2. If you are using SigV4, you can also include a service property that specifies the service name. Note: you need to npm install aws4 first.
    httpSignature: [Object], // options for the HTTP Signature Scheme using Joyent's library. The keyId and key properties must be specified.
    followRedirect: [Boolean, Function], // 允许你传递一个布尔类型的值直接告诉request是否跟踪重定向,默认true。还允许你传递一个Function根据需求觉定是否重定向。当你传递function时,此function有且只有一个参数,该参数是 response。
    followAllRedirects: [Boolean], // 配置是否跟踪非GET的HTTP3xx的重定向。
    followOriginalHttpMethod: [Boolean], //  你可以配置这个字段确定是否在其他请求方式下进行重定向跟踪。
    maxRedirects: [Boolean], // 重定向最大次数
    removeRefererHeader: [Boolean], // 默认false,在进行重定向时是否移除referer这个header参数到下一个请求。
    agentClass: [String], // alternatively specify your agent's class name
    agentOptions: [String], //
    forever: [Boolean], // set to true to use the forever-agent
    pool: [Object], //
    localAddress: [String], // local interface to bind for network connections
    proxy: [Object], // an HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for the url parameter (by embedding the auth info in the uri)
    strictSSL: [Boolean], // 是否强制验证证书。
    tunnel: [Boolean], // controls the behavior of HTTP CONNECT tunneling as follows:
    // undefined (default) - true if the destination is https, false otherwise
    // true - always tunnel to the destination by making a CONNECT request to the proxy
    // false - request the destination as a GET request.
    proxyHeaderWhiteList: [Array], // a whitelist of headers to send to a tunneling proxy
    proxyHeaderExclusiveList: [Array], // a whitelist of headers to send exclusively to a tunneling proxy and not to destination.
    time: [Boolean], // 如果设定为true,则会记录每次请求响应的时间,然后response对象则会新增以下字段:
     // [elapsedTime] - Duration of the entire request/response in milliseconds (deprecated).
     // [responseStartTime] - Timestamp when the response began (in Unix Epoch milliseconds) (deprecated).
     // [timingStart] - Timestamp of the start of the request (in Unix Epoch milliseconds).
     // [timings] - Contains event timestamps in millisecond resolution relative to timingStart. If there were redirects, the properties reflect the timings of the final request in the redirect chain:
     // [timings][socket] - Relative timestamp when the http module's socket event fires. This happens when the socket is assigned to the request.
     // [timings][lookup] - Relative timestamp when the net module's lookup event fires. This happens when the DNS has been resolved.
     // [timings][connect] - Relative timestamp when the net module's connect event fires. This happens when the server acknowledges the TCP connection.
     // [timings][response] - Relative timestamp when the http module's response event fires. This happens when the first bytes are received from the server.
     // [timings][end] - Relative timestamp when the last bytes of the response are received.
     // [timingPhases] - Contains the durations of each request phase. If there were redirects, the properties reflect the timings of the final request in the redirect chain:
     // [timingPhases][wait] - Duration of socket initialization (timings.socket)
     // [timingPhases][dns] - Duration of DNS lookup (timings.lookup - timings.socket)
     // [timingPhases][tcp] - Duration of TCP connection (timings.connect - timings.socket)
     // [timingPhases][firstByte] - Duration of HTTP server response (timings.response - timings.connect)
     // [timingPhases][download] - Duration of HTTP download (timings.end - timings.response)
     // [timingPhases][total] - Duration entire HTTP round-trip (timings.end)
    har: [Object], // a HAR 1.2 Request Object, will be processed from HAR format into options overwriting matching values (see the HAR 1.2 section for details).
    callback: [Function], // 允许你直接把回调写在配置里。这个回调函数有三个参数:
    // 1、error
    // 2、response
    // 3、body
}

四、方法列表

request提供了各种请求对应的方法:

request.get()     // "GET".
request.post()    // "POST".
request.put()     // "PUT".
request.patch()   // "PATCH".
request.del()     // "DELETE".
request.head()    // "HEAD".
request.options() // "OPTIONS".

除了这些还有下面的高级用法。

1、request.defaults(options)

这个方法可以理解为创建一个新的 request 对象,使用此方法后产生的request对象其行为规则和配置都是基于传递的option,就像这样:

let request = require("request");

let newRequest = request.defaults({
    baseUrl: "https://www.microanswer.cn"
});

// 使用基于指定配置的对象进行请求。
newRequest.get("/about", function (err, response, body) {
    // err 错误
    // response 响应对象
    // body 响应内容
});

2、request.cookie()

此方法创建一个新cookie.

request.cookie('key1=value1')

3、request.jar()

此方法可以创建一个处理cookie的工具。用法如下:

let request = require("request");
// 创建一个cookie工具jar。
let myjar = request.jar();

let url = "https://www.microanswer.cn";

request.get(url, {
    jar: myjar, // 将cookie工具传入,使cookie的处理变得简单。
}, function (err, response, body) {

    // 获取请求后返回的cookie。
    let cookieString = myjar.getCookieString(url);
    // "key1=value1; key2=value2; ..."

    // 获取请求后返回的cookie,以数组返回.
    let cookies = myjar.getCookies(url)
    // [{key: 'key1', value: 'value1', domain: "www.microanswer.com", ...}, ...]

});

五、关于response

response对象上你可以获取到这些信息:

response.statusCode; // 响应状态码
response.statusText; // 响应状态文案
response.headers; // 响应头,内容的键都是小写字母

六、进阶

由于request完整的支持了Stream的api,因此你可以吧requet当成一个流来使用,上文中提到的文件下载就是其用途之一,有了这个特性,request还能进行各种意想不到的的骚操作:

1、网络资源复制

你可以直接把一个网络地址的内容直接提交到另一个地址中:

request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'));

当然了,作为接收方,你必须要兼容纯steam流的内容输入。这样你才可以完整如此简便的操作。

2、文件上传

注意,这里文件上传并非标准form表单文件上传,如果你希望进行标准上传,请参看上面的文档。使用request实现了steam的api的特性,可以方便的实现文件上传:

fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))

3、像Stream那样操作

如果你习惯了操作Stream的话,那么下面的代码你一定非常熟悉:

request
  .get('http://google.com/img.png')
  // reponse 事件是 request 特别实现,当响应头接收完成时会发起该事件
  .on('response', function(response) {
    console.log("response");
    console.log(response.statusCode) // 200
    console.log(response.headers['content-type']) // 'image/png'
  })
  .on('error', function(err) {
    console.log(err)
  })
  .pipe(request.put('http://mysite.com/img.png'))

七、官网

了解request更多相关信息,请前往https://www.npmjs.com/package/request

Full text complete, Reproduction please indicate the source. Help you? Not as good as one:
Comment(Comments need to be logged in. You are not logged in.)
You need to log in before you can comment.

Comments (0 Comments)