upload json data to server with HTTPClient

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By piever
:warning: Old Version Published before Godot 3 was released.

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 = "http://10.40.11.166/"
var RESPONSE = HTTP.connect("10.40.11.166",80)

while(HTTP.get_status() == HTTPClient.STATUS_CONNECTING or HTTP.get_status() == HTTPClient.STATUS_RESOLVING):
	HTTP.poll()
	OS.delay_msec(300)
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())]
RESPONSE = HTTP.request(HTTPClient.METHOD_POST, url, HEADERS, QUERY)
assert(RESPONSE == OK)

while (HTTP.get_status() == HTTPClient.STATUS_REQUESTING):
	HTTP.poll()
	OS.delay_msec(300)
#    # 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:

json_encode($_REQUEST)

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.

Thanks!

What do you mean by screw up?

Pipe | 2017-07-12 21:45

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: How to use HTTP Client to upload an image to server - Archive - Godot Forum

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")

piever | 2017-07-12 22:39

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.

Pipe | 2017-07-13 13:28

:bust_in_silhouette: Reply From: jeff

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"]
RESPONSE = HTTP.request(HTTPClient.METHOD_POST, url, HEADERS, QUERY)