http.get()
Convenience method for an HTTP GET request that auto-ends the request.
Syntax
http.get(url[, options][, callback]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
url | string | URL | Yes | The URL to request. |
options | object | No | Standard request options (headers, agent, etc.). |
callback | function | No | Called with the IncomingMessage response. |
Returns
http.ClientRequest — The request object; it is already ended.
Examples
import { get } from 'node:http';
get('http://localhost:3000/time', (res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => console.log(data));
});
Output
2026-05-15T10:00:00Z
import { get } from 'node:http';
get('http://localhost:3000/x', (res) => {
if (res.statusCode !== 200) {
res.resume(); // drain to free the socket
console.log('error', res.statusCode);
}
});
Output
error 404
Notes
Identical to `http.request` but sets method GET and calls `end()`
for you. Always consume or `resume()` the response stream even on
errors, otherwise the socket is not released. Prefer `fetch` for new
code.