ClientRequest
This object is created internally and returned from {@link request}. It represents an in-progress request whose header has already been queued. The header is still mutable using the setHeader(name, value)
, getHeader(name)
, removeHeader(name)
API. The actual header will be sent along with the first data chunk or when calling request.end()
.
To get the response, add a listener for 'response'
to the request object. 'response'
will be emitted from the request object when the response headers have been received. The 'response'
event is executed with one argument which is an instance of {@link IncomingMessage}.
During the 'response'
event, one can add listeners to the response object; particularly to listen for the 'data'
event.
If no 'response'
handler is added, then the response will be entirely discarded. However, if a 'response'
event handler is added, then the data from the response object must be consumed, either by calling response.read()
whenever there is a 'readable'
event, or by adding a 'data'
handler, or by calling the .resume()
method. Until the data is consumed, the 'end'
event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error.
For backward compatibility, res
will only emit 'error'
if there is an 'error'
listener registered.
Set Content-Length
header to limit the response body size. If response.strictContentLength
is set to true
, mismatching the Content-Length
header value will result in an Error
being thrown, identified by code:``'ERR_HTTP_CONTENT_LENGTH_MISMATCH'
.
Content-Length
value should be in bytes, not characters. Use Buffer.byteLength()
to determine the length of the body in bytes.
Since
v0.1.17
Properties
Alias of outgoingMessage.socket
.
Read-only. true
if the headers were sent, otherwise false
.
Limits maximum response headers count. If set to 0, no limit will be applied.
Is true
if it is safe to call writable.write()
, which means the stream has not been destroyed, errored, or ended.
When sending request through a keep-alive enabled agent, the underlying socket might be reused. But if server closes connection at unfortunate time, client may run into a 'ECONNRESET' error.
Number of times writable.uncork()
needs to be called in order to fully uncork the stream.
Is true
after writable.end()
has been called. This property does not indicate whether the data has been flushed, for this use writable.writableFinished
instead.
Is set to true
immediately before the 'finish'
event is emitted.
Return the value of highWaterMark
passed when creating this Writable
.
This property contains the number of bytes (or objects) in the queue ready to be written. The value provides introspection data regarding the status of the highWaterMark
.
Is true
if the stream's buffer has been full and stream will emit 'drain'
.
Getter for the property objectMode
of a given Writable
stream.
Functions
Adds HTTP trailers (headers but at the end of the message) to the message.
Append a single header value to the header object.
Destroy the stream. Optionally emit an 'error'
event, and emit a 'close'
event (unless emitClose
is set to false
). After this call, the writable stream has ended and subsequent calls to write()
or end()
will result in an ERR_STREAM_DESTROYED
error. This is a destructive and immediate way to destroy a stream. Previous calls to write()
may not have drained, and may trigger an ERR_STREAM_DESTROYED
error. Use end()
instead of destroy if data should flush before close, or wait for the 'drain'
event before destroying the stream.
Calling the writable.end()
method signals that no more data will be written to the Writable
. The optional chunk
and encoding
arguments allow one final additional chunk of data to be written immediately before closing the stream.
Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbol
s.
Flushes the message headers.
Returns an array containing the unique names of the current outgoing headers. All names are lowercase.
Returns a shallow copy of the current outgoing headers. Since a shallow copy is used, array values may be mutated without additional calls to various header-related HTTP module methods. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.
Returns the current max listener value for the EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to {@link defaultMaxListeners}.
Returns an array containing the unique names of the current outgoing raw headers. Header names are returned with their exact casing being set.
Removes a header that is queued for implicit sending.
The writable.setDefaultEncoding()
method sets the default encoding
for a Writable
stream.
Sets a single header value. If the header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings to send multiple headers with the same name.
By default EventEmitter
s will print a warning if more than 10
listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners()
method allows the limit to be modified for this specific EventEmitter
instance. The value can be set to Infinity
(or 0
) to indicate an unlimited number of listeners.
Once a socket is assigned to this request and is connected socket.setNoDelay()
will be called.
Once a socket is assigned to this request and is connected socket.setKeepAlive()
will be called.
Once a socket is assigned to this request and is connected socket.setTimeout()
will be called.
The writable.write()
method writes some data to the stream, and calls the supplied callback
once the data has been fully handled. If an error occurs, the callback
will be called with the error as its first argument. The callback
is called asynchronously and before 'error'
is emitted.