0 votes
func HTTPRequest(host, page, dictionary_data):
var errorJSON = {}
errorJSON['error'] = Error.ERROR_CONNECTION_FAILED

var err = 0
var http = HTTPClient.new()

err = http.connect_to_host(host, 80)

if(err != OK):
    return errorJSON

while (http.get_status() == HTTPClient.STATUS_CONNECTING or 
            http.get_status() == HTTPClient.STATUS_RESOLVING):
        http.poll()
        OS.delay_msec(500)

if (http.get_status() != HTTPClient.STATUS_CONNECTED):
    return errorJSON

var headers = [
    "User-Agent: Pirulo/1.0 (Godot)",
    "Content-Type: application/json",
    "Accept: */*"
]

var queryString = http.query_string_from_dict(dictionary_data)
err = http.request(HTTPClient.METHOD_POST, page, headers, queryString)

if(err != OK):
    return errorJSON

while (http.get_status() == HTTPClient.STATUS_REQUESTING):
    http.poll()
    OS.delay_msec(500)

if(http.get_status() != HTTPClient.STATUS_BODY and 
http.get_status() != HTTPClient.STATUS_CONNECTED):
    return errorJSON

if (!http.has_response() or http.get_response_code() != 200):
    return errorJSON

var rb = PoolByteArray()

while (http.get_status() == HTTPClient.STATUS_BODY):
    http.poll()
    var chunk = http.read_response_body_chunk()
    if chunk.size() == 0:
        OS.delay_usec(1000)
    else:
        rb = rb + chunk

var text = rb.get_string_from_ascii()

return text

Hi. My function is in a Singleton class named Game. And I'm calling this func like this:

func button_down_login():
    var host = "http://www.rabidstreets.test"
    var page = "/en/game/login"
    var dictionary_data = {}
    dictionary_data['username'] = lineEditUsername.get_text()
    dictionary_data['password'] = lineEditPassword.get_text()
    var data = Game.HTTPRequest(host, page, dictionary_data)
    print(data)

And it prints nothing. Can you help me?

asked Oct 12, 2018 in Engine by Sphynxinator (27 points)

1 Answer

0 votes
Best answer
var queryString = http.query_string_from_dict(dictionary_data)
err = http.request(HTTPClient.METHOD_POST, page, headers, queryString)

Just fix this part as:

var body = {}
body["username"] = "user"
body = JSON.print(body)
err = http.request(HTTPClient.METHOD_POST, page, headers, body)
answered Oct 13, 2018 by Sphynxinator (27 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.