- 类: ClientRequest
- new ClientRequest(options)
- 实例事件
- Event: 'response'
- 事件: "login"
- Event: 'finish'
- Event: 'abort'
- Event: 'error'
- 事件: 'close'
- Event: 'redirect'
- 实例属性
- request.chunkedEncoding
- 实例方法
- request.setHeader(name, value)
- request.getHeader(name)
- request.removeHeader(name)
- request.write(chunk[, encoding][, callback])
- request.end([chunk][, encoding][, callback])
- request.abort()
- request.followRedirect()
- request.getUploadProgress()
类: ClientRequest
发起HTTP/HTTPS请求.
线程:主线程
ClientRequest实现了Writable Stream接口, 因此是一个EventEmitter类型.
new ClientRequest(options)
参数(Object | String) -如果选项是一个String类型, 它被解释为请求的URL. 如果它是一个Object类型, 那么它可以通过以下属性指定一个HTTP请求:methodString (可选) - HTTP请求方法. 默认为GET方法.urlString (可选) - 请求的URL. 必须在指定了http或https的协议方案的独立表单中提供.sessionObject (可选) - 与请求相关联的Session实例.partitionString (可选) - 与请求相关联的partition名称. 默认为空字符串.session选项优先于partition选项. 因此, 如果session是显式指定的, 则partition将被忽略.protocolString (可选) - 在"scheme:"表单中的协议方案. 目前支持的值为'http:' 或者'https:'. 默认为'http:'.hostString (可选) - 作为连接提供的服务器主机,主机名和端口号'hostname:port'.hostnameString (可选) - 服务器主机名.portInteger (可选) - 服务器侦听的端口号.pathString (可选) - 请求URL的路径部分.redirectString (可选) - 请求的重定向模式. 可选值为follow,error或manual. 默认值为follow. 当模式为error时, 重定向将被终止. 当模式为manual时,表示延迟重定向直到调用了request.followRedirect。 在此模式中侦听redirect事件,以获得关于重定向请求的更多细节。options属性,如protocol,host,hostname,port和path,在 URL 模块中会严格遵循 Node.js 的模式
例如,我们可以创建与github.com相同的请求如下:
const request = net.request({method: 'GET',protocol: 'https:',hostname: 'github.com',port: 443,path: '/'})
实例事件
Event: 'response'
返回:
response收到的消息 - 表示HTTP响应消息的对象。
事件: "login"
返回:
authInfoObjectisProxyBooleanschemeStringhostStringportIntegerrealmString
callbackFunction - 回调函数usernameStringpasswordString
当身份验证代理请求用户认证时触发
用户证书会调用 callback方法:
usernameStringpasswordString
request.on('login', (authInfo, callback) => {callback('username', 'password')})
提供空的凭证将取消请求,并在响应对象上报告一个身份验证错误:
request.on('response', (response) => {console.log(`STATUS: ${response.statusCode}`);response.on('error', (error) => {console.log(`ERROR: ${JSON.stringify(error)}`)})})request.on('login', (authInfo, callback) => {callback()})
Event: 'finish'
在 request 最终的 chunk 数据后写入 request 后触发
Event: 'abort'
当 request请求被中止时发出。如果request 请求已经关闭, abort中止事件将不会被触发。
Event: 'error'
返回:
errorError -提供失败信息的错误对象。
当net网络模块没有发出网络请求时会触发。 通常情况下,当request请求对象发出一个error错误事件时,一个close关闭事件会随之发生,并且不会提供响应对象。
事件: 'close'
作为HTTP 的 request-response 中的最后一个事件发出。 close事件表明,在request或response 对象中不会发出更多的事件。
Event: 'redirect'
返回:
statusCodeIntegermethodStringredirectUrlStringresponseHeadersObject
当发出重定,并且模式为manuals(手动)时触发。调用request.followRedirect将持续重定向
实例属性
request.chunkedEncoding
一个Boolean类型的值,指定请求是否将使用 HTTP 分块传输编码。 默认值为 false. 该属性是可读写的, 但它只能在第一次写入操作之前设置,因为还没有写入 HTTP 头。 在第一写入后如果设置chunkedEncoding属性将引发错误。
如果 request 的 body 是一个大的数据时,强烈建议使用块编码。因为数据将以小块的方式进行传输, 而不是在 Electron 进程内存中内部缓冲。
实例方法
request.setHeader(name, value)
nameString - 额外的 HTTP 头名称.valueObject - 额外的 HTTP 头的值.
添加一个额外的 HTTP 头。 头名称发出时是大写的. 它只能在第一次写入之前调用。 在第一次写入后调用此方法将引发错误。 如果传递的值不是String, 则会调用toString ()方法来获取最终值。
request.getHeader(name)
nameString - 指定一个额外的头名称.
返回Object-以前设置的额外标头名称的值。
request.removeHeader(name)
nameString - 指定一个额外的头名称.
删除以前设置的额外头名称。此方法只能在首次写入之前调用。尝试在第一次写入后调用它会引发错误。
request.write(chunk[, encoding][, callback])
chunk(String | Buffer) - 请求主体数据的一个块。如果是字符串, 它将使用指定的编码转换Buffer。encodingString(可选)-用于将字符串块转换为Buffer对象。默认值为 "utf-8"。callbackFunction (可选)-在写操作结束后调用。callback实质上是为了保持与 Node.js API 的相似性而引入的虚拟函数。 在将chunk内容传递到 Chromium 网络层之后, 在下一个 tick 中异步调用。 与 Node.js 实现相反, 不保证chunk内容在调用callback之前已经被刷新。
向请求正文中添加一个数据块。 第一次写操作可能导致在线路上发出请求头。 在第一次写入操作后, 不允许添加或删除自定义标头。
request.end([chunk][, encoding][, callback])
chunk(String | Buffer) (可选)encodingString (可选)callbackFunction (可选)
发送请求数据的最后一个块。将不允许后续的写入或结束操作。finish事件将在结束操作后发出。
request.abort()
取消正在进行的 HTTP 事务。 如果请求已发出 close 事件, 则中止操作将不起作用。 否则正在进行的事件将发出 abort 和 close 事件。 此外, 如果有一个正在进行的响应对象, 它将发出 aborted 事件。
request.followRedirect()
当重定向模式为 manual 手动 时, 将继续延迟的重定向请求。
request.getUploadProgress()
返回 Object:
activeBoolean - Whether the request is currently active. If this is false no other properties will be setstartedBoolean - Whether the upload has started. If this is false bothcurrentandtotalwill be set to 0.currentInteger - The number of bytes that have been uploaded so fartotalInteger - The number of bytes that will be uploaded this request
You can use this method in conjunction withPOSTrequests to get the progress of a file upload or other data transfer.
