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 onlyaborted:Bool

The request.aborted property will be true if the request has been aborted.

read onlyconnection:Socket

See request.socket.

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

read onlypath:String

The request path.

read onlysocket:Socket

Reference to the underlying socket. Usually users will not want to access this property. In particular, the socket will not emit 'readable' events because of how the protocol parser attaches to the socket. The socket may also be accessed via request.connection.

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().

removeHeader(name:String):Void

Removes a header that's already defined into headers object.

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.

Inherited Variables

Defined by Writable

read onlydestroyed:Bool

Is true after writable.destroy() has been called.

See also:

read onlyisTTY:Bool

Terminal write streams (i.e. process.stdout) have this property set to true. It is false for any other write streams.

See also:

read onlywritable:Bool

Is true if it is safe to call writable.write().

See also:

read onlywritableEnded:Bool

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.

See also:

read onlywritableFinished:Bool

Is set to true immediately before the 'finish' event is emitted.

See also:

read onlywritableLength:Int

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.

See also:

read onlywritableObjectMode:Bool

Getter for the property objectMode of a given Writable stream.

See also:

read onlywritablehighWaterMark:Int

Return the value of highWaterMark passed when constructing this Writable.

See also:

Inherited Methods

Defined by Writable

cork():Void

The writable.cork() method forces all written data to be buffered in memory. The buffered data will be flushed when either the stream.uncork() or stream.end() methods are called.

See also:

destroy(?error:Error):TSelf

Destroy the stream. Optionally emit an 'error' event, and emit a 'close' event unless emitClose is set in 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. Implementors should not override this method, but instead implement writable._destroy().

See also:

end(chunk:Dynamic, ?encoding:String, ?callback:EitherType<() ‑> Void, Null<Error> ‑> Void>):Void

end(?callback:EitherType<() ‑> Void, Null<Error> ‑> Void>):Void

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. If provided, the optional callback function is attached as a listener for the 'finish' event.

See also:

setDefaultEncoding(encoding:String):TSelf

The writable.setDefaultEncoding() method sets the default encoding for a Writable stream.

See also:

uncork():Void

The writable.uncork() method flushes all data buffered since stream.cork() was called.

See also:

write(chunk:Dynamic, ?encoding:String, ?callback:EitherType<() ‑> Void, Null<Error> ‑> Void>):Bool

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 may or may not be called with the error as its first argument. To reliably detect write errors, add a listener for the 'error' event.

See also:

Defined by EventEmitter

addListener<T>(eventName:Event<T>, listener:T):TSelf

emit<T>(eventName:Event<T>, args:Rest<Dynamic>):Bool

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

See also:

eventNames():Array<EitherType<String, Symbol>>

Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings or Symbols.

See also:

getMaxListeners():Int

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.

See also:

listenerCount<T>(eventName:Event<T>):Int

Returns the number of listeners listening to the event named eventName.

See also:

listeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName.

See also:

off<T>(eventName:Event<T>, listener:T):TSelf

on<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

once<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

See also:

prependListener<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

prependOnceListener<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

See also:

rawListeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

See also:

removeAllListeners<T>(?eventName:Event<T>):TSelf

Removes all listeners, or those of the specified eventName.

See also:

removeListener<T>(eventName:Event<T>, listener:T):TSelf

Removes the specified listener from the listener array for the event named eventName.

See also:

setMaxListeners(n:Int):Void

By default EventEmitters 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. Obviously, not all events should be limited to just 10 listeners. 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.

See also: