request
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, an ERR_HTTP2_GOAWAY_SESSION
is thrown.
This method is only available if http2session.type
is equal to http2.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
\=/
Since
v8.4.0