clienthttp2session.request(headers[, options])


  • headers HTTP/2 请求头对象|HTTP/2 原始请求头
  • options <Object>
    • endStream <boolean> true 如果 Http2Stream 的可写端应该最初关闭,例如在发送不应预期有有效负载主体的 GET 请求时。
    • exclusive <boolean> 当值为 trueparent 指定了一个父流时,创建的流将成为父流的唯一直接依赖,所有其他现有的依赖将成为新创建流的依赖。 默认值: false
    • parent <number> 指定新创建的流所依赖的流的数字标识符。
    • waitForTrailers <boolean> 当设置为 true 时,Http2Stream 会在最后一个 DATA 帧发送后触发 'wantTrailers' 事件。
    • signal <AbortSignal> 一个可以用来中止正在进行的请求的 AbortSignal。
  • 返回:<ClientHttp2Stream>

仅适用于 HTTP/2 客户端的 Http2Session 实例,http2session.request() 会创建并返回一个 Http2Stream 实例,可用于向已连接的服务器发送 HTTP/2 请求。

【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.】

ClientHttp2Session 刚创建时,套接字可能尚未连接。如果在此期间调用 clienthttp2session.request(),实际的请求将被延迟,直到套接字准备好。如果在实际请求执行之前 session 被关闭,将抛出 ERR_HTTP2_GOAWAY_SESSION 错误。

【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.】

仅当 http2session.type 等于 http2.constants.NGHTTP2_SESSION_CLIENT 时,此方法才可用。

【This method is only available if http2session.type is equal to http2.constants.NGHTTP2_SESSION_CLIENT.】

import { connect, constants } from 'node:http2';
const clientSession = connect('https://localhost:1234');
const {
  HTTP2_HEADER_PATH,
  HTTP2_HEADER_STATUS,
} = 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', () => { /* .. */ });
});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', () => { /* .. */ });
});

当设置了 options.waitForTrailers 选项时,'wantTrailers' 事件会在最后一块要发送的有效负载数据排队后立即触发。然后可以调用 http2stream.sendTrailers() 方法向对端发送尾部头信息。

【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.】

当设置 options.waitForTrailers 时,Http2Stream 在发送最后一个 DATA 帧后不会自动关闭。用户代码必须调用 http2stream.sendTrailers()http2stream.close() 来关闭 Http2Stream

【When options.waitForTrailers is set, the Http2Stream will not automatically close when the final DATA frame is transmitted. User code must call either http2stream.sendTrailers() or http2stream.close() to close the Http2Stream.】

当使用 AbortSignal 设置 options.signal,然后调用相应 AbortControllerabort 时,请求将发出带有 AbortError 错误的 'error' 事件。

【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.】

:method:path 伪头未在 headers 中指定,它们分别默认为:

【The :method and :path pseudo-headers are not specified within headers, they respectively default to:】

  • :method = 'GET'
  • :path = /