How can I make a HTTP request with callback in C# ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arthur Attout

I cannot get an HTTP Request to work properly using C# in Godot.

Neither could I find an example of an HTTP Request that uses the _on_HTTPRequest_request_completed signal. Connecting the signal to a C# script through the editor doesn’t work, I already created an issue on this.

The fact that it isn’t working through the editor shouldn’t prevent me from connecting the signal manually, but I couldn’t find any example for this.

Has anyone ever done that ?
Thanks !

:bust_in_silhouette: Reply From: Mehmet0zen

Here is a quick example;

public override void _Ready()
    {
        HTTPRequest httpRequest = new HTTPRequest();
        AddChild(httpRequest);

    httpRequest.Connect("request_completed", this, "HTTPRequestCompleted");
    var testConnection = httpRequest.Request("request_link");
    if (testConnection != Error.Ok) {
        GD.PushError("An error occurred in the HTTP request.");
    }
}

public void HTTPRequestCompleted(int result, int response_code, string[] headers, byte[] body) {
    var response = JSON.Parse(Encoding.UTF8.GetString(body)).Result as Dictionary;
    item = response["items"][0];
    GD.Print(item);
}

Let me know if there are any other problems.