http.request()
Makes an outgoing HTTP request and returns a writable request stream.
Syntax
http.request(url[, options][, callback]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
url | string | URL | object | Yes | The target URL or an options object with `hostname`, `port`, `path`, `method`, and `headers`. |
options | object | No | Request options when the first argument is a URL. |
callback | function | No | Called with the IncomingMessage response. |
Returns
http.ClientRequest — A writable stream for the request body.
Examples
import { request } from 'node:http';
const req = request('http://localhost:3000/api', { method: 'POST' }, (res) => {
let body = '';
res.on('data', (c) => (body += c));
res.on('end', () => console.log(res.statusCode, body));
});
req.write(JSON.stringify({ name: 'Ada' }));
req.end();
Output
201 {"id":1}
Notes
You must call `req.end()` to send the request. For most code the
global `fetch()` (stable in Node 21) is simpler; reach for
`http.request` when you need streaming control or agents. Use
`https.request` for TLS.