For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server-net/classes/HttpClient.md.

Class: HttpClient

network

Beta

Example

simpleHttpRequest.ts

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

Methods

cancelAll()

cancelAll(reason): void

Early Execution Beta

Parameters

reason

string

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

Returns

void

Remarks

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

Cancels all pending requests.


get()

get(uri): Promise<HttpResponse>

Early Execution Beta

Parameters

uri

string

请求的 URL。

URL to make an HTTP Request to.

Returns

Promise<HttpResponse>

解析到对应响应的 Promise。

An awaitable promise that contains the HTTP response.

Remarks

发起一个 HTTP GET 请求。

Performs a simple HTTP get request.


request()

request(config): Promise<HttpResponse>

Early Execution Beta

Parameters

config

HttpRequest

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

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

Returns

Promise<HttpResponse>

解析到对应响应的 Promise。

An awaitable promise that contains the HTTP response.

Remarks

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

Example

simpleHttpRequest.ts

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

同领域相关