HTTP

http.get()

Convenience request function for GET that calls end for you. The response is still a stream, so read or explicitly discard it to release the connection. HTTP error statuses are not automatically request errors. Tested on Node.js 22.16.0. Example output (one line per entry): ["503"].

Formal Syntax & Structure

nodejs syntaxnodejs
http.get(url[, options][, callback]); http.get(options[, callback])

Parameters & Attributes

NameTypeDescription
urlstring | URLDestination in URL overload.
optionsobjectConnection or request settings.
callbackFunctionResponse listener.

Return Value

http.ClientRequest

The outgoing request.

Working Example

Example Snippetnodejs
import http from 'node:http';const server = http.createServer((req, res) => { res.statusCode = 503; res.end('busy'); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try {    await new Promise((resolve, reject) => { const q = http.get(url, r => { r.resume(); r.on('end', () => { console.log(r.statusCode); resolve(); }); r.on('error', reject); }); q.on('error', reject); });}finally {    server.closeAllConnections();    await new Promise(r => server.close(r));}
Node.js Reference