Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Class

ClientRequest

http.ClientRequest is the writable stream representing an in-progress HTTP client request. It is returned by http.request(), accepts request data and headers, and emits the server response when response headers arrive.

Since
v0.1.17

How it works

ClientRequest is the writable stream returned by http.request() while an HTTP request is in progress. Configure its headers or write request data, listen for the response and errors, then call end() to complete a request that has no more body data.

Syntax

class http.ClientRequest

Returns

An instance of the http.ClientRequest class is returned by http.request().

Key points

  • Applications normally obtain ClientRequest instances from http.request(), rather than constructing them directly.
  • The object tracks an HTTP request that has started and has queued its headers.
  • Headers can be inspected, changed, or removed through the request's header methods while they remain mutable.
  • Request bodies can be supplied by writing data to the ClientRequest stream.
  • Listen for response to begin processing the server's response after its headers arrive.
  • The response event provides an IncomingMessage containing the incoming HTTP response.

Common mistakes

  • Header changes must be made before the first body chunk is written or before end() sends the request.
  • The response is delivered through the request's response event rather than as a direct return value.
  • For uploads, write request-body data to the ClientRequest because it is the writable side of the operation.

Example: Make an HTTP request

const http = require('node:http');

const request = http.request('http://example.com', (response) => {
  response.on('data', (chunk) => process.stdout.write(chunk));
  response.on('end', () => console.log('\nDone'));
});

request.on('error', console.error);
request.end();

This creates a ClientRequest through http.request(), consumes the response, handles request errors, and ends the request.

When to use it

HTTP client calls

Send HTTP requests and process their responses asynchronously.

Request-body uploads

Upload form data, JSON, or file contents in a request body.

Header management

Set, inspect, or remove request headers before transmission.

  • http.request()
  • http.IncomingMessage
  • setHeader(name, value)
  • getHeader(name)
  • removeHeader(name)
  • request.end()

Source: HTTP. Checked 2026-10-15.