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.
maximilianosinski:
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.
System.Net.Http.HttpClient should work fine - if it doesn’t, you’re likely hitting a bug and should report that with a repro script.
As to the C# runtime missing PerformHttpRequest, that’s because it’s a function implemented by the Lua scripting runtime, as shown by the docs page being in the Lua section.
Finally, PerformHttpRequestInternal should work fine assuming you correctly set up the result event - what have you tried there to end up with a conclusion of it not working?
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.
system
Closed
June 23, 2022, 3:30am
3
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.