This object is created internally and returned from http.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 http.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.
Unlike the request
object, if the response closes prematurely, the response object does not emit an 'error'
event
but instead emits the 'aborted'
event.
Node.js does not check whether Content-Length and the length of the body which has been transmitted are equal or not.
Variables
read onlyfinished:Bool
The response.finished
property will be true if response.end()
has been called.
maxHeadersCount:Null<Int>
Limits maximum response headers count. If set to 0, no limit will be applied.
Default: 2000
Methods
abort():Void
Marks the request as aborting. Calling this will cause remaining data in the response to be dropped and the socket to be destroyed.
flushHeaders():Void
Flush the request headers.
For efficiency reasons, node.js normally buffers the request headers until you call request.end()
or write the first chunk of request data. It then tries hard to pack the request headers and data
into a single TCP packet.
That's usually what you want (it saves a TCP round-trip) but not when the first data isn't sent
until possibly much later. flushHeaders
lets you bypass the optimization and kickstart the request.
getHeader(name:String):EitherType<String, Array<String>>
Reads out a header on the request. The name is case-insensitive.
The type of the return value depends on the arguments provided to request.setHeader()
.
setHeader(name:String, value:String):Void
setHeader(name:String, value:Array<String>):Void
Sets a single header value for headers object.
If this header already exists in the to-be-sent headers, its value will be replaced.
Use an array of strings here to send multiple headers with the same name.
Non-string values will be stored without modification. Therefore, request.getHeader()
may return non-string values.
However, the non-string values will be converted to strings for network transmission.
setNoDelay(?noDelay:Bool):Void
Once a socket is assigned to this request and is connected
socket.setNoDelay
will be called.
setSocketKeepAlive(enable:Bool, ?initialDelay:Int):Void
setSocketKeepAlive(?initialDelay:Int):Void
Once a socket is assigned to this request and is connected
socket.setKeepAlive
() will be called.
setTimeout(timeout:Int, ?callback:Socket ‑> Void):ClientRequest
Once a socket is assigned to this request and is connected socket.setTimeout()
will be called.