class ClientRequest
Usage in Deno
```typescript import { ClientRequest } from "node:node__http.d.ts"; ```> [!WARNING] Deno compatibility
> Constructor option `createConnection` is not supported.
This object is created internally and returned from [request](../.././node__http.d.ts/~/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](../.././node__http.d.ts/~/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.
new
ClientRequest(url: ,cb?: (res: IncomingMessage) => void,)
deprecated
aborted: boolean
The `request.aborted` property will be `true` if the request has
been aborted.
host: string
The request host.
maxHeadersCount: number
Limits maximum response headers count. If set to 0, no limit will be applied.
method: string
The request method.
path: string
The request path.
protocol: string
The request protocol.
reusedSocket: boolean
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.
```js
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.
```js
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();
```
deprecated
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.
deprecated
addListener(event: "abort",listener: () => void,): this
addListener(event: "connect",listener: () => void,): this
addListener(event: "continue",listener: () => void,): this
addListener(event: "information",listener: (info: InformationEvent) => void,): this
addListener(event: "response",listener: (response: IncomingMessage) => void,): this
addListener(event: "socket",listener: (socket: Socket) => void,): this
addListener(event: "timeout",listener: () => void,): this
addListener(event: "upgrade",listener: () => void,): this
addListener(event: "close",listener: () => void,): this
addListener(event: "drain",listener: () => void,): this
addListener(event: "error",listener: (err: Error) => void,): this
addListener(event: "finish",listener: () => void,): this
addListener(event: "pipe",listener: (src: stream.Readable) => void,): this
addListener(event: "unpipe",listener: (src: stream.Readable) => void,): this
addListener(event: string | symbol,listener: (...args: any[]) => void,): this
getRawHeaderNames(): string[]
Returns an array containing the unique names of the current outgoing raw
headers. Header names are returned with their exact casing being set.
```js
request.setHeader('Foo', 'bar');
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
const headerNames = request.getRawHeaderNames();
// headerNames === ['Foo', 'Set-Cookie']
```
deprecated
on(event: "abort",listener: () => void,): this
on(event: "connect",listener: () => void,): this
on(event: "continue",listener: () => void,): this
on(event: "information",listener: (info: InformationEvent) => void,): this
on(event: "response",listener: (response: IncomingMessage) => void,): this
on(event: "timeout",listener: () => void,): this
on(event: "upgrade",listener: () => void,): this
on(event: "close",listener: () => void,): this
on(event: "drain",listener: () => void,): this
on(event: "error",listener: (err: Error) => void,): this
on(event: "finish",listener: () => void,): this
on(event: "pipe",listener: (src: stream.Readable) => void,): this
on(event: "unpipe",listener: (src: stream.Readable) => void,): this
on(event: string | symbol,listener: (...args: any[]) => void,): this
deprecated
once(event: "abort",listener: () => void,): this
once(event: "connect",listener: () => void,): this
once(event: "continue",listener: () => void,): this
once(event: "information",listener: (info: InformationEvent) => void,): this
once(event: "response",listener: (response: IncomingMessage) => void,): this
once(event: "timeout",listener: () => void,): this
once(event: "upgrade",listener: () => void,): this
once(event: "close",listener: () => void,): this
once(event: "drain",listener: () => void,): this
once(event: "error",listener: (err: Error) => void,): this
once(event: "finish",listener: () => void,): this
once(event: "pipe",listener: (src: stream.Readable) => void,): this
once(event: "unpipe",listener: (src: stream.Readable) => void,): this
once(event: string | symbol,listener: (...args: any[]) => void,): this
deprecated
prependListener(event: "abort",listener: () => void,): this
prependListener(event: "connect",listener: () => void,): this
prependListener(event: "continue",listener: () => void,): this
prependListener(event: "information",listener: (info: InformationEvent) => void,): this
prependListener(event: "response",listener: (response: IncomingMessage) => void,): this
prependListener(event: "socket",listener: (socket: Socket) => void,): this
prependListener(event: "timeout",listener: () => void,): this
prependListener(event: "upgrade",listener: () => void,): this
prependListener(event: "close",listener: () => void,): this
prependListener(event: "drain",listener: () => void,): this
prependListener(event: "error",listener: (err: Error) => void,): this
prependListener(event: "finish",listener: () => void,): this
prependListener(event: "pipe",listener: (src: stream.Readable) => void,): this
prependListener(event: "unpipe",listener: (src: stream.Readable) => void,): this
prependListener(event: string | symbol,listener: (...args: any[]) => void,): this
deprecated
prependOnceListener(event: "abort",listener: () => void,): this
prependOnceListener(event: "connect",listener: () => void,): this
prependOnceListener(event: "continue",listener: () => void,): this
prependOnceListener(event: "information",listener: (info: InformationEvent) => void,): this
prependOnceListener(event: "response",listener: (response: IncomingMessage) => void,): this
prependOnceListener(event: "socket",listener: (socket: Socket) => void,): this
prependOnceListener(event: "timeout",listener: () => void,): this
prependOnceListener(event: "upgrade",listener: () => void,): this
prependOnceListener(event: "close",listener: () => void,): this
prependOnceListener(event: "drain",listener: () => void,): this
prependOnceListener(event: "error",listener: (err: Error) => void,): this
prependOnceListener(event: "finish",listener: () => void,): this
prependOnceListener(event: "pipe",listener: (src: stream.Readable) => void,): this
prependOnceListener(event: "unpipe",listener: (src: stream.Readable) => void,): this
prependOnceListener(event: string | symbol,listener: (...args: any[]) => void,): this
setNoDelay(noDelay?: boolean): void
Once a socket is assigned to this request and is connected `socket.setNoDelay()` will be called.
setSocketKeepAlive(enable?: boolean,initialDelay?: number,): void
Once a socket is assigned to this request and is connected `socket.setKeepAlive()` will be called.
setTimeout(timeout: number,callback?: () => void,): this
Once a socket is assigned to this request and is connected `socket.setTimeout()` will be called.