简体   繁体   中英

LibGDX HTTP Post request recieving 400 message on Node.js server

I'm working on a game using the LibGDX library. One part of the game involves collecting game data and sending it to a server as a JSON array to be recorded in a database. I'm using Node as my server but I'm running into an issue every time the game sends a POST request to the server. I'm using LibGDX's Http.Net library to send the request.

I keep getting a HTTP 400 error message and data isn't being recorded. Attached are screenshots of the relevant code and messages. Thank you!

Images: https://imgur.com/a/CF1U6#0

I don't have enough reputation to insert images, sorry.

I figured out the problem. I was using LibGDX's included JSON library to construct my JSON String. However, when I created Json json = new Json(); , it defaults to writing minimal (I think). Names are not surrounded by double quotes in this format. See: https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/JsonWriter.OutputType.html

The solution is to set it to Json json = new Json(JsonWriter.OutputType.json); . This will format it as JSON which Express will recognize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM