Usage in Deno
import { type ClientHttp2Session } from "node:http2";
request(headers?: OutgoingHttpHeaders,options?: ClientSessionRequestOptions,): ClientHttp2Stream
For HTTP/2 Client Http2Session
instances only, the http2session.request()
creates and returns an Http2Stream
instance that can be used to send an
HTTP/2 request to the connected server.
When a ClientHttp2Session
is first created, the socket may not yet be
connected. if clienthttp2session.request()
is called during this time, the
actual request will be deferred until the socket is ready to go.
If the session
is closed before the actual request be executed, anERR_HTTP2_GOAWAY_SESSION
is thrown.
This method is only available if http2session.type
is equal tohttp2.constants.NGHTTP2_SESSION_CLIENT
.
const http2 = require('node:http2'); const clientSession = http2.connect('https://localhost:1234'); const { HTTP2_HEADER_PATH, HTTP2_HEADER_STATUS, } = http2.constants; const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' }); req.on('response', (headers) => { console.log(headers[HTTP2_HEADER_STATUS]); req.on('data', (chunk) => { // .. }); req.on('end', () => { // .. }); });
When the options.waitForTrailers
option is set, the 'wantTrailers'
event
is emitted immediately after queuing the last chunk of payload data to be sent.
The http2stream.sendTrailers()
method can then be called to send trailing
headers to the peer.
When options.waitForTrailers
is set, the Http2Stream
will not automatically
close when the final DATA
frame is transmitted. User code must call eitherhttp2stream.sendTrailers()
or http2stream.close()
to close theHttp2Stream
.
When options.signal
is set with an AbortSignal
and then abort
on the
corresponding AbortController
is called, the request will emit an 'error'
event with an AbortError
error.
The :method
and :path
pseudo-headers are not specified within headers
,
they respectively default to:
:method
='GET'
:path
=/
addListener(event: "altsvc",listener: (alt: string,origin: string,stream: number,) => void,): this
addListener(event: "origin",listener: (origins: string[]) => void,): this
addListener(event: "connect",listener: (session: ClientHttp2Session,socket: net.Socket | tls.TLSSocket,) => void,): this
addListener(event: "stream",listener: () => void,): this
addListener(event: string | symbol,listener: (...args: any[]) => void,): this
emit(event: "altsvc",alt: string,origin: string,stream: number,): boolean
emit(event: "origin",origins: readonly string[],): boolean
emit(): boolean
emit(event: "stream",stream: ClientHttp2Stream,headers: IncomingHttpHeaders & IncomingHttpStatusHeader,flags: number,): boolean
emit(event: string | symbol,...args: any[],): boolean
on(event: "altsvc",listener: (alt: string,origin: string,stream: number,) => void,): this
on(event: "origin",listener: (origins: string[]) => void,): this
on(event: "connect",listener: (session: ClientHttp2Session,socket: net.Socket | tls.TLSSocket,) => void,): this
on(event: "stream",listener: () => void,): this
on(event: string | symbol,listener: (...args: any[]) => void,): this
once(event: "altsvc",listener: (alt: string,origin: string,stream: number,) => void,): this
once(event: "origin",listener: (origins: string[]) => void,): this
once(event: "connect",listener: (session: ClientHttp2Session,socket: net.Socket | tls.TLSSocket,) => void,): this
once(event: "stream",listener: () => void,): this
once(event: string | symbol,listener: (...args: any[]) => void,): this
prependListener(event: "altsvc",listener: (alt: string,origin: string,stream: number,) => void,): this
prependListener(event: "origin",listener: (origins: string[]) => void,): this
prependListener(event: "connect",listener: (session: ClientHttp2Session,socket: net.Socket | tls.TLSSocket,) => void,): this
prependListener(event: "stream",listener: () => void,): this
prependListener(event: string | symbol,listener: (...args: any[]) => void,): this
prependOnceListener(event: "altsvc",listener: (alt: string,origin: string,stream: number,) => void,): this
prependOnceListener(event: "origin",listener: (origins: string[]) => void,): this
prependOnceListener(event: "connect",listener: (session: ClientHttp2Session,socket: net.Socket | tls.TLSSocket,) => void,): this
prependOnceListener(event: "stream",listener: () => void,): this
prependOnceListener(event: string | symbol,listener: (...args: any[]) => void,): this