类 HttpRequest实验性

包含构成 HTTP 请求所需的参数。主要用于发起请求。

Main object for structuring a request.

import { HttpRequest, HttpHeader, HttpRequestMethod, http } from '@minecraft/server-net';

async function updateScore() {
const req = new HttpRequest('http://localhost:3000/updateScore');

req.body = JSON.stringify({
score: 22,
});

req.method = HttpRequestMethod.Post;
req.headers = [
new HttpHeader('Content-Type', 'application/json'),
new HttpHeader('auth', 'my-auth-token'),
];

await http.request(req);
}

构造函数

  • 实验性

    参数

    • uri: string

    返回 HttpRequest

属性

body: string

请求的携带的主体负载内容。

Content of the body of the HTTP request.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

headers: HttpHeader[]

该请求的请求标头集合。

A collection of HTTP headers to add to the outbound request.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

该请求的请求方法(例如 GET、PUT 或 PATCH)。

HTTP method (e.g., GET or PUT or PATCH) to use for making the request.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

timeout: number

在请求被视为超时而取消前的最大时长,单位为秒。

Amount of time, in seconds, before the request times out and is abandoned.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

uri: string

请求访问的资源 URI。

The HTTP resource to access.

无法在只读模式下修改此属性,详见 WorldBeforeEvents

方法

  • 实验性

    参数

    返回 HttpRequest

    向当前请求中增加一个请求标头。

    Adds an additional header to the overall list of headers used in the corresponding HTTP request.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

  • 实验性

    参数

    • body: string

    返回 HttpRequest

    将请求的主体负载内容设定为指定内容。

    Updates the content of the body of the HTTP request.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

  • 实验性

    参数

    返回 HttpRequest

    使用指定的请求标头集合增加或替换请求中指定的请求标头。

    Replaces and applies a set of HTTP Headers for the request.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

  • 实验性

    参数

    返回 HttpRequest

    设置该请求的请求方法(例如 GET、PUT 或 PATCH)。

    Sets the desired HTTP method (e.g., GET or PUT or PATCH) to use for making the request.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents

  • 实验性

    参数

    • timeout: number

    返回 HttpRequest

    设置该请求的最大超时时间,单位为秒。

    无法在只读模式下调用此函数,详见 WorldBeforeEvents