MicrosoftDocs / minecraft-creator

This is the repository for Minecraft Bedrock documentation.
Creative Commons Attribution 4.0 International
184 stars 132 forks source link

[@minecraft/server-net] Server loop blocks when not including a request body #861

Open ProtocolPav opened 5 months ago

ProtocolPav commented 5 months ago

Describe the bug When I try to send any of POST, PUT, DELETE requests using server-net, if I do not include a request body, the game crashes. I believe the game loop gets blocked since entities stop moving, blocks stop dropping themselves when mined, and I can't even send commands via the console.

To Reproduce Steps to reproduce the behavior:

  1. Create a script that sends a DELETE request to an API endpoint
  2. Do not include a request body
  3. Notice how entities stop moving
  4. Also, notice on your API access logs that the request never came in

Expected behavior DELETE request gets sent to the API and you don't crash your server

Screenshots Console logs. Sends the requests. Notice how there is no response. image

Changed the script code to include an empty JSON body. Notice the response. image

Additional context Simple code that accesses all 4 methods on an endpoint in the API and returns the response body. Technically, DELETE requests shouldn't even have a request body so I never set one, and was so confused why it would crash.

import { http, HttpHeader, HttpRequest, HttpRequestMethod } from '@minecraft/server-net';

system.run(() => {
    const requests = [
        new HttpRequest(`http://nexuscore:8000/api/v0.1/users/test`).setMethod(HttpRequestMethod.Get),
        new HttpRequest(`http://nexuscore:8000/api/v0.1/users/test`).setMethod(HttpRequestMethod.Put),
        new HttpRequest(`http://nexuscore:8000/api/v0.1/users/test`).setMethod(HttpRequestMethod.Post),
        new HttpRequest(`http://nexuscore:8000/api/v0.1/users/test`).setMethod(HttpRequestMethod.Delete),
    ]

    for (const request of requests) {
        request.body = JSON.stringify({}) // If you do not include this line, POST, PUT and DELETE requests crash the game
        request.headers = [
            new HttpHeader("Content-Type", "application/json"),
            new HttpHeader("auth", "my-auth-token"),
        ];

        console.log(request.uri, request.method)
        http.request(request).then((response) => console.log(response.body))
    }
})