HTTP/2

clientHttp2Session.request()

Creates a new stream on an established client session. Consume or cancel every response and handle session-level failures separately. This example uses cleartext HTTP/2 on loopback only, not public TLS negotiation. Tested on Node.js 22.16.0. Example output (one line per entry): ["/a,/b"].

Formal Syntax & Structure

nodejs syntaxnodejs
session.request(headers[, options])

Parameters & Attributes

NameTypeDescription
headersobjectRequest pseudo-headers and headers.
optionsobjectStream configuration.

Return Value

ClientHttp2Stream

Request and response stream.

Working Example

Example Snippetnodejs
import { createServer, connect, constants } from 'node:http2';import { once } from 'node:events';import { text } from 'node:stream/consumers';const server = createServer();server.on('stream', (stream, headers) => { stream.respond({ ':status': 200 }); stream.end(headers[':path']); });server.listen(0, '127.0.0.1');await once(server, 'listening');const client = connect('http://127.0.0.1:' + server.address().port);try {    await once(client, 'connect');    const a = client.request({ ':path': '/a' });    const b = client.request({ ':path': '/b' });    a.end();    b.end();    console.log((await Promise.all([text(a), text(b)])).join(','));}finally {    const closed = once(client, 'close');    client.destroy();    await closed;    await new Promise(resolve => server.close(resolve));}
Node.js Reference