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/HttpRequest.md.

Class: HttpRequest

network

Beta

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

Main object for structuring a 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);
}

Constructors

Constructor

new HttpRequest(uri): HttpRequest

Beta

Parameters

uri

string

Returns

HttpRequest

Properties

body

body: any

Early Execution Beta

Remarks

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

Content of the body of the HTTP request.


headers

headers: HttpHeader[]

Early Execution Beta

Remarks

该请求的请求标头集合。

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


method

method: HttpRequestMethod

Early Execution Beta

Remarks

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

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


timeout

timeout: number

Early Execution Beta

Remarks

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

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


uri

uri: string

Early Execution Beta

Remarks

请求访问的资源 URI。

The HTTP resource to access.

Methods

addHeader()

addHeader(key, value): HttpRequest

Early Execution Beta

Parameters

key

string

value

any

Returns

HttpRequest

Remarks

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

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


setBody()

setBody(body): HttpRequest

Early Execution Beta

Parameters

body

any

Returns

HttpRequest

Remarks

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

Updates the content of the body of the HTTP request.


setHeaders()

setHeaders(headers): HttpRequest

Early Execution Beta

Parameters

headers

HttpHeader[]

Returns

HttpRequest

Remarks

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

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


setMethod()

setMethod(method): HttpRequest

Early Execution Beta

Parameters

method

HttpRequestMethod

Returns

HttpRequest

Remarks

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

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


setTimeout()

setTimeout(timeout): HttpRequest

Early Execution Beta

Parameters

timeout

number

The timeout value, in seconds.

Returns

HttpRequest

Remarks

Sets the maximum amount of time, in seconds, before the request times out and is cancelled.

设置请求超时并被取消前的最大时间(以秒为单位)。

同领域相关