类 HttpClient实验性

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);
}

方法

  • 实验性

    参数

    • reason: string

      提供给请求对应的 Promise 的 reject 原因。

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

    返回 void

    取消所有尚未完成的请求。

    Cancels all pending requests.

  • 实验性

    参数

    • config: HttpRequest

      用于发起请求的配置对象。

      Contains an HTTP Request object with configuration data on the HTTP request.

    返回 Promise<HttpResponse>

    解析到对应响应的 Promise。

    An awaitable promise that contains the HTTP response.

    发起一个 HTTP 请求。 Performs an HTTP request.

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

    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);
    }