+2 votes

I'm developing a game in Godot and I'd like to get some playing statistics. As of now I can save them locally to a json file and I'd like to upload the file to a server after the game is over.

This code taken basically from here allows me to send a dict to the server:

var HTTP = HTTPClient.new()
var url = ""
var RESPONSE = HTTP.connect("",80)

while(HTTP.get_status() == HTTPClient.STATUS_CONNECTING or HTTP.get_status() == HTTPClient.STATUS_RESOLVING):
assert(HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
var data = {"a": 2, "b" : 3}
var QUERY = HTTP.query_string_from_dict(data)
var HEADERS = ["User-Agent: Pirulo/1.0 (Godot)", "Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(QUERY.length())]
assert(RESPONSE == OK)

while (HTTP.get_status() == HTTPClient.STATUS_REQUESTING):
#    # Make sure request finished
assert(HTTP.get_status() == HTTPClient.STATUS_BODY or HTTP.get_status() == HTTPClient.STATUS_CONNECTED)

which I can then convert to json with:


From my PHP server.

Unfortunately, as soon as the dict is more complex (i.e. a dict of dicts or a dict of arrays) the json formatting screws up.

What's the best way to proceed? Is there a simple way to send a named list of arrays or a more complex data structure to a php server?

Alternatively, what is a combination of GDScript with HTTPClient and php file on server side that would allow me to upload a file from "user://" to server? Some "proof of concept" combination of both client side and server side that can upload a file would help me a lot! I've seen this but I couldn't get it to work.


in Engine by (26 points)

What do you mean by screw up?

For example the dict of arrays:

{"a" : [1,2], "b" = ["string1", "string2"]}

gets reformatted into

 {"a" : "[1,2]", "b" = "[string1, string2]"}

which in general could be painful to parse (or at least a regular JSON parser would fail).
I've come to the conclusion that it should actually be easier to save the dict as JSON locally and then upload the file, but I'm not sure how to upload a file with GDScript to a PHP server. I tried the script I linked above: https://godotengine.org/qa/15434/how-to-use-http-client-to-upload-an-image-to-server

but I get an empty $_FILES on the server, so I was curious to see if there is a proof of concept example of uploading a file to server from Godot (using POST method and "Content-Type: multipart/form-data")

Better JSON parsing in GDScript

A very requested feature, now JSON supports parsing into any datatype (not just >dictionaries), as new built-in functions were added to GDScript to handle it.

It seems like they are improving JSON in 3.0, so you might wanna wait for it.

1 Answer

+2 votes

For anyone who still needs help with this problem, you just need to specify the Content Type in the header as JSON.

var QUERY = data.to_json()
var HEADERS = ["User-Agent: Jeff", "Content-Type: application/json"]
by (58 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.