Constructor option createConnection is not supported.
This object is created internally and returned from 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 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.
Constructors Jump to heading
Jump to headingClientRequest(url: ,cb?: (res: IncomingMessage) => void,)Properties Jump to heading
Jump to headingaborted: booleanThe request.aborted property will be true if the request has
been aborted.
Jump to headinghost: stringThe request host.
Jump to headingmaxHeadersCount: numberLimits maximum response headers count. If set to 0, no limit will be applied.
Jump to headingmethod: stringThe request method.
Jump to headingpath: stringThe request path.
Jump to headingprotocol: stringThe request protocol.
Jump to headingreusedSocket: booleanWhen 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.
import http from 'node:http';
// Server has a 5 seconds keep-alive timeout by default
http
.createServer((req, res) => {
res.write('hello\n');
res.end();
})
.listen(3000);
setInterval(() => {
// Adapting a keep-alive agent
http.get('http://localhost:3000', { agent }, (res) => {
res.on('data', (data) => {
// Do nothing
});
});
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
By marking a request whether it reused socket or not, we can do automatic error retry base on it.
import http from 'node:http';
const agent = new http.Agent({ keepAlive: true });
function retriableRequest() {
const req = http
.get('http://localhost:3000', { agent }, (res) => {
// ...
})
.on('error', (err) => {
// Check if retry is needed
if (req.reusedSocket && err.code === 'ECONNRESET') {
retriableRequest();
}
});
}
retriableRequest();
Methods Jump to heading
Jump to headingabort(): voidMarks the request as aborting. Calling this will cause remaining data in the response to be dropped and the socket to be destroyed.
Jump to headingaddListener(event: "abort",listener: () => void,): thisJump to headingaddListener(event: "connect",listener: () => void,): thisJump to headingaddListener(event: "continue",listener: () => void,): thisJump to headingaddListener(event: "information",listener: (info: InformationEvent) => void,): thisJump to headingaddListener(event: "response",listener: (response: IncomingMessage) => void,): thisJump to headingaddListener(event: "socket",listener: (socket: Socket) => void,): thisJump to headingaddListener(event: "timeout",listener: () => void,): thisJump to headingaddListener(event: "upgrade",listener: () => void,): thisJump to headingaddListener(event: "close",listener: () => void,): thisJump to headingaddListener(event: "drain",listener: () => void,): thisJump to headingaddListener(event: "error",listener: (err: Error) => void,): thisJump to headingaddListener(event: "finish",listener: () => void,): thisJump to headingaddListener(event: "pipe",listener: (src: stream.Readable) => void,): thisJump to headingaddListener(event: "unpipe",listener: (src: stream.Readable) => void,): thisJump to headingaddListener(event: string | symbol,listener: (...args: any[]) => void,): thisJump to headinggetRawHeaderNames(): string[]Returns an array containing the unique names of the current outgoing raw headers. Header names are returned with their exact casing being set.
request.setHeader('Foo', 'bar');
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
const headerNames = request.getRawHeaderNames();
// headerNames === ['Foo', 'Set-Cookie']
Jump to headingon(event: "abort",listener: () => void,): thisJump to headingon(event: "connect",listener: () => void,): thisJump to headingon(event: "continue",listener: () => void,): thisJump to headingon(event: "information",listener: (info: InformationEvent) => void,): thisJump to headingon(event: "response",listener: (response: IncomingMessage) => void,): thisJump to headingon(event: "socket",listener: (socket: Socket) => void,): thisJump to headingon(event: "timeout",listener: () => void,): thisJump to headingon(event: "upgrade",listener: () => void,): thisJump to headingon(event: "close",listener: () => void,): thisJump to headingon(event: "drain",listener: () => void,): thisJump to headingon(event: "error",listener: (err: Error) => void,): thisJump to headingon(event: "finish",listener: () => void,): thisJump to headingon(event: "pipe",listener: (src: stream.Readable) => void,): thisJump to headingon(event: "unpipe",listener: (src: stream.Readable) => void,): thisJump to headingon(event: string | symbol,listener: (...args: any[]) => void,): thisJump to headingonSocket(socket: Socket): voidJump to headingonce(event: "abort",listener: () => void,): thisJump to headingonce(event: "connect",listener: () => void,): thisJump to headingonce(event: "continue",listener: () => void,): thisJump to headingonce(event: "information",listener: (info: InformationEvent) => void,): thisJump to headingonce(event: "response",listener: (response: IncomingMessage) => void,): thisJump to headingonce(event: "socket",listener: (socket: Socket) => void,): thisJump to headingonce(event: "timeout",listener: () => void,): thisJump to headingonce(event: "upgrade",listener: () => void,): thisJump to headingonce(event: "close",listener: () => void,): thisJump to headingonce(event: "drain",listener: () => void,): thisJump to headingonce(event: "error",listener: (err: Error) => void,): thisJump to headingonce(event: "finish",listener: () => void,): thisJump to headingonce(event: "pipe",listener: (src: stream.Readable) => void,): thisJump to headingonce(event: "unpipe",listener: (src: stream.Readable) => void,): thisJump to headingonce(event: string | symbol,listener: (...args: any[]) => void,): thisJump to headingprependListener(event: "abort",listener: () => void,): thisJump to headingprependListener(event: "connect",listener: () => void,): thisJump to headingprependListener(event: "continue",listener: () => void,): thisJump to headingprependListener(event: "information",listener: (info: InformationEvent) => void,): thisJump to headingprependListener(event: "response",listener: (response: IncomingMessage) => void,): thisJump to headingprependListener(event: "socket",listener: (socket: Socket) => void,): thisJump to headingprependListener(event: "timeout",listener: () => void,): thisJump to headingprependListener(event: "upgrade",listener: () => void,): thisJump to headingprependListener(event: "close",listener: () => void,): thisJump to headingprependListener(event: "drain",listener: () => void,): thisJump to headingprependListener(event: "error",listener: (err: Error) => void,): thisJump to headingprependListener(event: "finish",listener: () => void,): thisJump to headingprependListener(event: "pipe",listener: (src: stream.Readable) => void,): thisJump to headingprependListener(event: "unpipe",listener: (src: stream.Readable) => void,): thisJump to headingprependListener(event: string | symbol,listener: (...args: any[]) => void,): thisJump to headingprependOnceListener(event: "abort",listener: () => void,): thisJump to headingprependOnceListener(event: "connect",listener: () => void,): thisJump to headingprependOnceListener(event: "continue",listener: () => void,): thisJump to headingprependOnceListener(event: "information",listener: (info: InformationEvent) => void,): thisJump to headingprependOnceListener(event: "response",listener: (response: IncomingMessage) => void,): thisJump to headingprependOnceListener(event: "socket",listener: (socket: Socket) => void,): thisJump to headingprependOnceListener(event: "timeout",listener: () => void,): thisJump to headingprependOnceListener(event: "upgrade",listener: () => void,): thisJump to headingprependOnceListener(event: "close",listener: () => void,): thisJump to headingprependOnceListener(event: "drain",listener: () => void,): thisJump to headingprependOnceListener(event: "error",listener: (err: Error) => void,): thisJump to headingprependOnceListener(event: "finish",listener: () => void,): thisJump to headingprependOnceListener(event: "pipe",listener: (src: stream.Readable) => void,): thisJump to headingprependOnceListener(event: "unpipe",listener: (src: stream.Readable) => void,): thisJump to headingprependOnceListener(event: string | symbol,listener: (...args: any[]) => void,): thisJump to headingsetNoDelay(noDelay?: boolean): voidOnce a socket is assigned to this request and is connected socket.setNoDelay() will be called.
Jump to headingsetSocketKeepAlive(enable?: boolean,initialDelay?: number,): voidOnce a socket is assigned to this request and is connected socket.setKeepAlive() will be called.
Jump to headingsetTimeout(timeout: number,callback?: () => void,): thisOnce a socket is assigned to this request and is connected socket.setTimeout() will be called.