mojira.dev
BDS-18627

[Script API] Headers/Body not set on BDS GET HttpRequest calls on Linux, but is on Windows

See comment: this is only a difference between how Windows and Linux handle GET requests.

Running a BDS server on Linux Ubuntu 20.04.6 LTS, utilizing the gametest @minecraft/server-net.HttpRequest method, and noticing that headers and request payloads are not sent from BDS on Linux while they are sent from BDS on Windows. Both client and server code is the same, and I can confirm that requests are reaching the server from the BDS client on both platforms.

Running netcat on Linux shows the following:

 

GET /write_state HTTP/1.1
Host: 127.0.0.1:8888
Accept: */*
User-Agent: libhttpclient/1.0.0.0

While running ncat on Windows shows the following:

 

 

GET /update HTTP/1.1
Connection: Keep-Alive
User-Agent: libhttpclient/1.0.0.0
Content-Length: 53
Host: 127.0.0.1:8888

{"time":5000,"weather":0,"entities":{},"messages":[]}

Note the content length header and the payload (both requests should have a payload).

 

Code sample:

 

const req = new mcnet.HttpRequest(HOST + endpoint);
  req.setTimeout(0.5);
  if (body !== undefined) req.setBody(body);
  if (headers !== undefined) {
    const headerObjs: mcnet.HttpHeader[] = [];
    for (const [k, v] of Object.entries(headers)) {
      headerObjs.push(new mcnet.HttpHeader(k, v));
    }
    req.setHeaders(headerObjs);
  }
  let res;
  try {
    res = await mcnet.http.request(req);
  } catch(e) {
    console.error('Invalid request: ', e);
    return;
  }

Let me know if there is anything that would help narrow this down. Thanks!

 

Comments 3

Updated title to reflect the more accurate case: this is only a difference on GET requests on Linux vs Windows. 

Test Code used:

import * as mc from "@minecraft/server";
import * as mcnet from "@minecraft/server-net";

async function timeout(ticks: number) {
  return await new Promise<void>(res => {
    mc.system.runTimeout(res, ticks);
  });
}

async function testMsg() {
  async function sendReq(method: mcnet.HttpRequestMethod) {
    const req = new mcnet.HttpRequest('http://httpbin.org/anything');
    req.setHeaders([new mcnet.HttpHeader('X-Custom', 'foo')]);
    req.setBody('Test test test.')
    req.setMethod(method)
    const res = await mcnet.http.request(req);
    console.log('Status:', res.status)
    console.log('Headers:', JSON.stringify(res.headers.map(h => [h.key, h.value])));
    console.log('Body:', res.body);
    console.log('.');
    console.log('.');
    console.log('.');
  }
  await sendReq(mcnet.HttpRequestMethod.Get);
  await timeout(60);
  await sendReq(mcnet.HttpRequestMethod.Post);
  await timeout(60);
  await sendReq(mcnet.HttpRequestMethod.Put);
  await timeout(60);
  await sendReq(mcnet.HttpRequestMethod.Delete);
}

testMsg();

Post, Delete, and Put all behaved identically.

Response (Windows) (note the data field with "Test test test"):

[2023-08-07 14:21:09:246 WARN] Web response body of unsupported content-type 'application/json' is being treated as 'text/plain'
[2023-08-07 14:21:09:280 INFO] [Scripting] Status: 200

[2023-08-07 14:21:09:282 INFO] [Scripting] Headers: [["access-control-allow-origin","*"],["access-control-allow-credentials","true"],["date","Mon, 07 Aug 2023 21:21:08 GMT"],["connection","keep-alive"],["content-type","application/json"],["content-length","398"],["server","gunicorn/19.9.0"]]

[2023-08-07 14:21:09:285 INFO] [Scripting] Body: {
  "args": {},
  "data": "Test test test.",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "15",
    "Host": "httpbin.org",
    "User-Agent": "libhttpclient/1.0.0.0",
    "X-Amzn-Trace-Id": "Root=1-64d16044-1c5754f16c0b822c13e4e88c",
    "X-Custom": "foo"
  },
  "json": null,
  "method": "GET",
  "origin": "<REDACTED>",
  "url": "http://httpbin.org/anything"
}

Response Linux (note the empty data field):

[2023-08-07 21:40:31:341 WARN] Web response body of unsupported content-type 'application/json' is being treated as 'text/plain'
[2023-08-07 21:40:31:376 INFO] [Scripting] Status: 200

[2023-08-07 21:40:31:377 INFO] [Scripting] Headers: [["server","gunicorn/19.9.0"],["date","Mon, 07 Aug 2023 21:40:31 GMT"],["content-type","application/json"],["connection","keep-alive"],["content-length","378"],["access-control-allow-origin","*"],["access-control-allow-credentials","true"]]

[2023-08-07 21:40:31:377 INFO] [Scripting] Body: {
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "libhttpclient/1.0.0.0",
    "X-Amzn-Trace-Id": "Root=1-64d164cf-488979347ad6527362eca3f5",
    "X-Custom": "foo"
  },
"json": null,
"method": "GET",
"origin": "<REDACTED>",
"url": "http://httpbin.org/anything"
}

Expected behavior here is that the two platforms should operate the same, either both allowing GET requests to have attached bodies, or for neither to allow bodies and to explicitly throw an exception when set.

Hi

Does this issue still occur after updating to 1.20.15?

This ticket will automatically reopen when you reply.

Cleaning up old tickets: This ticket had been set to 'Awaiting Response', but has not received a response from the reporter (~3 months+) so is being closed as Incomplete. If you feel this is still a valid issue then please comment, or create a new ticket following the Issue Guidelines which includes steps to reproduce the problem.

Quick Links:
📓 Issue Guidelines – 💬 Mojang Support – 📧 Suggestions – 📖 Minecraft Wiki

stevarino

(Unassigned)

Unconfirmed

Retrieved