Skip to content
Cloudflare Docs

http

Agent

An implementation of the Node.js `http.Agent' class.

An Agent manages HTTP connection reuse by maintaining request queues per host/port. In the workers environment, however, such low-level management of the network connection, ports, etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the implementation of Agent in Workers is a stub implementation that does not support connection pooling or keep-alive.

import { Agent } from 'node:http';
import { strictEqual } from 'node:assert';
const agent = new Agent();
strictEqual(agent.protocol, 'http:');

get

An implementation of the Node.js http.get method.

The get method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTP GET requests without manually configuring request options.

import { get } from 'node:http';
import { strictEqual, ok } from 'node:assert';
get('http://docs.cloudflare.com/robots.txt', (res) => {
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
strictEqual(res.statusCode, 301);
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
ok(data.includes('301 Moved Permanently'));
});
});

request

An implementation of the Node.js `http.request' method.

The request method creates an HTTP request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a writable stream for sending request data.

import { request } from 'node:http';
import { strictEqual } from 'node:assert';
const req = request({
method: 'GET',
protocol: 'http:',
hostname: 'docs.cloudflare.com',
path: '/'
}, (res) => {
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
strictEqual(res.statusCode, 301);
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
ok(data.includes('301 Moved Permanently'));
});
});
req.end();
const req = request(new URL('http://docs.cloudflare.com'),{
method: 'GET',
}, (res) => {
// requests to http://docs.cloudflare.com get redirected to their https counterpart.
strictEqual(res.statusCode, 301);
});
req.end();

The following options passed to the request method are not supported due to the differences in the Cloudflare Workers and the implementation of the node:http module:

  • maxHeaderSize
  • insecureHTTPParser
  • createConnection
  • lookup
  • socketPath

OutgoingMessage

The OutgoingMessage class represents an HTTP response that is sent to the client. It provides methods for writing response headers and body, as well as for ending the response. OutgoingMessage extends from the Writable stream class.

import { OutgoingMessage } from 'node:http';
const res = new OutgoingMessage();
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Hello, World!');
res.end();

IncomingMessage

The IncomingMessage class represents an HTTP request that is received from the client. It provides methods for reading request headers and body, as well as for ending the request. IncomingMessage extends from the Readable stream class.

import { get, IncomingMessage } from 'node:http';
import { ok, strictEqual } from 'node:assert';
get('http://docs.cloudflare.com', (res) => {
strictEqual(res.statusCode, 301);
ok(res instanceof IncomingMessage);
});