How-To: Make a GET / POST Request with the HttpClient from System.Net.Http

Hello, im trying to make a GET Request to a .JSON file stored on my webserver. Im trying to make a GET Request from the ClientMain.cs, with System.Net.Http.HttpClient, but that didn’t work, so i tried it on the ServerMain.cs, but it didnt work too, then i tried it System.Net.HttpWebRequest, but that doesn’t work too. Does someone know what the problem might be?

On the Client Resource, it gives me the error: BadImageFormat.

And on The Server Resource it doesnt even tells me the error, it just outputs that it didnt work.

Thanks for any help.

I can’t speak to HttpClient not working, as we’ve never used it, but this is what we use and it works for us.

public static Dictionary<int, Dictionary<string, dynamic>> responseDictionary;

public Main()
{
    responseDictionary = new Dictionary<int, Dictionary<string, dynamic>>();

    EventHandlers["__cfx_internal:httpResponse"] += new Action<int, int, string, dynamic>(Response);
}

public void Response(int token, int status, string text, dynamic header)
{
    Dictionary<string, dynamic> response = new Dictionary<string, dynamic>
    {
        ["headers"] = header,
        ["status"] = status,
        ["content"] = text
    };

    responseDictionary[token] = response;
}

...

RequestDataInternal requestData = new RequestDataInternal
{
    url = "https://my.website.api/v1/hello",
    method = "GET",
    data = "Stringified JSON",
    headers = new Dictionary<string, string>()
    {
        { "Content-Type", "application/json" }
    }
};

try
{
    int timeout = 100; // 10 seconds
    string json = Json.Stringify(requestData);
    int token = API.PerformHttpRequestInternal(json, json.Length);

    while (!responseDictionary.ContainsKey(token) && timeout > 0)
    {
        timeout--;
        await Delay(100);
    }

    if (timeout == 0)
    {
        return;
    }

    Dictionary<string, dynamic> res = responseDictionary[token];

    responseDictionary.Remove(token);

    return res;
}
catch
{
    //
}

Copied from our code, may not work out of the box for you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.