clienthttp2session.request(headers[, options])
-
headers
<HTTP/2 Headers Object> -
options
<Object>-
endStream
<boolean>true
如果Http2Stream
可写端最初应关闭,例如发送不应期望有效负载正文的GET
请求时。¥
endStream
<boolean>true
if theHttp2Stream
writable side should be closed initially, such as when sending aGET
request that should not expect a payload body. -
exclusive
<boolean> 当true
和parent
标识父流时,创建的流将成为父流的唯一直接依赖,所有其他现有依赖都成为新创建流的依赖。默认值:false
。¥
exclusive
<boolean> Whentrue
andparent
identifies a parent Stream, the created stream is made the sole direct dependency of the parent, with all other existing dependents made a dependent of the newly created stream. Default:false
. -
parent
<number> 指定新创建的流所依赖的流的数字标识符。¥
parent
<number> Specifies the numeric identifier of a stream the newly created stream is dependent on. -
weight
<number> 指定流相对于具有相同parent
的其他流的相对依赖。该值为1
到256
(含)之间的数字。¥
weight
<number> Specifies the relative dependency of a stream in relation to other streams with the sameparent
. The value is a number between1
and256
(inclusive). -
waitForTrailers
<boolean> 当为true
时,Http2Stream
将在发送完最后的DATA
帧后触发'wantTrailers'
事件。¥
waitForTrailers
<boolean> Whentrue
, theHttp2Stream
will emit the'wantTrailers'
event after the finalDATA
frame has been sent. -
signal
<AbortSignal> 可用于中止正在进行的请求的中止信号。¥
signal
<AbortSignal> An AbortSignal that may be used to abort an ongoing request.
-
-
¥Returns: <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
,则传输完最后的 DATA
帧时,Http2Stream
不会自动关闭。用户代码必须调用 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
.
当 options.signal
设置为 AbortSignal
,然后调用相应 AbortController
上的 abort
时,则请求将使用 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
=/