进程:

    ClientRequest实现了Writable Stream接口, 因此是一个类型.

    options 属性,如 protocol, host, hostname, portpath,在 URL 模块中会严格遵循 Node.js 的模式

    例如,我们可以创建与github.com相同的请求如下:

    事件: ‘response’

    返回:

    • response 收到的消息 - 表示HTTP响应消息的对象。

    事件: “login”

    返回:

    • authInfo Object
      • isProxy Boolean
      • scheme String
      • host String
      • port Integer
      • realm String
    • callback Function
      • username String (optional)

    当身份验证代理请求用户认证时触发

    用户证书会调用 callback方法:

    • username String
    • password String

    事件:’finish’

    request 最终的 chunk 数据后写入 request 后触发

    事件: ‘abort’

    Emitted when the request is aborted. The abort event will not be fired if the request is already closed.

    事件: ‘error’

    返回:

    • error Error -提供失败信息的错误对象。

    net网络模块没有发出网络请求时会触发。 通常情况下,当 request请求对象发出一个 error错误事件时,一个 close关闭事件会随之发生,并且不会提供响应对象。

    事件: ‘close’

    作为HTTP 的 request-response 中的最后一个事件发出。 close事件表明,在requestresponse 对象中不会发出更多的事件。

    事件: ‘redirect’

    返回:

    • statusCode Integer
    • method String
    • redirectUrl String
    • responseHeaders Record

    Emitted when the server returns a redirect response (e.g. 301 Moved Permanently). Calling request.followRedirect will continue with the redirection. If this event is handled, must be called synchronously, otherwise the request will be cancelled.

    一个Boolean类型的值,指定请求是否将使用 HTTP 分块传输编码。 默认值为 false. 该属性是可读写的, 但它只能在第一次写入操作之前设置,因为还没有写入 HTTP 头。 在第一写入后如果设置chunkedEncoding属性将引发错误。

    request.setHeader(name, value)

    • name String - 额外的 HTTP 头名称.
    • value String - An extra HTTP header value.

    添加一个额外的 HTTP 头。 The header name will be issued as-is without lowercasing. 它只能在第一次写入之前调用。 在第一次写入后调用此方法将引发错误。 如果传递的值不是 String, 则会调用 toString () 方法来获取最终值。

    request.getHeader(name)

    • name String - 指定一个额外的头名称.

    Returns String - The value of a previously set extra header name.

    request.removeHeader(name)

    • name String - 指定一个额外的头名称.

    Removes a previously set extra header name. This method can be called only before first write. Trying to call it after the first write will throw an error.

    request.write(chunk[, encoding][, callback])

    • chunk (String | Buffer) - A chunk of the request body’s data. If it is a string, it is converted into a Buffer using the specified encoding.
    • encoding String (optional) - Used to convert string chunks into Buffer objects. Defaults to ‘utf-8’.
    • callback Function (可选)-在写操作结束后调用。

    callback 实质上是为了保持与 Node.js API 的相似性而引入的虚拟函数。 在将 chunk 内容传递到 Chromium 网络层之后, 在下一个 tick 中异步调用。 与 Node.js 实现相反, 不保证 chunk 内容在调用 callback 之前已经被刷新。

    向请求正文中添加一个数据块。 第一次写操作可能导致在线路上发出请求头。 在第一次写入操作后, 不允许添加或删除自定义标头。

    request.end([chunk][, encoding][, callback])

    • chunk (String | Buffer) (可选)
    • encoding String (可选)
    • callback Function (可选)

    Sends the last chunk of the request data. Subsequent write or end operations will not be allowed. The finish event is emitted just after the end operation.

    request.abort()

    取消正在进行的 HTTP 事务。 如果请求已发出 close 事件, 则中止操作将不起作用。 否则正在进行的事件将发出 abortclose 事件。 此外, 如果有一个正在进行的响应对象, 它将发出 aborted 事件。

    request.followRedirect()

    Continues any pending redirection. Can only be called during a 'redirect' event.

    request.getUploadProgress()

    • active Boolean - Whether the request is currently active. If this is false no other properties will be set
    • started Boolean - Whether the upload has started. If this is false both current and total will be set to 0.
    • current Integer - The number of bytes that have been uploaded so far

    You can use this method in conjunction with POST requests to get the progress of a file upload or other data transfer.