Skip to main content

Class: HttpClient

Example

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

new HttpClient()

private new HttpClient(): HttpClient

Returns

HttpClient

Methods

cancelAll()

cancelAll(reason): void

Parameters

ParameterType
reasonstring

Returns

void

Remarks

Cancels all pending requests.

This function can't be called in read-only mode.


get()

get(uri): Promise ↗️ <HttpResponse>

Parameters

ParameterTypeDescription
uristringURL to make an HTTP Request to.

Returns

Promise ↗️ <HttpResponse>

An awaitable promise that contains the HTTP response.

Remarks

Performs a simple HTTP get request.

This function can't be called in read-only mode.


request()

request(config): Promise ↗️ <HttpResponse>

Parameters

ParameterTypeDescription
configHttpRequestContains an HTTP Request object with configuration data on
the HTTP request.

Returns

Promise ↗️ <HttpResponse>

An awaitable promise that contains the HTTP response.

Remarks

Performs an HTTP request.

This function can't be called in read-only mode.

Example

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