I was trying to use some server's API functions, and suddenly came to this error. I will explain it after the code sample:
public IEnumerator Post(Dictionary<string, string> data)
{
var request = UnityWebRequest.Post(Url, data);
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
yield return request.Send();
if (request.isError)
UpdateNetworkLog("Network error has occured: " + request.error);
else
// Some code after success
}
As you can see, mostly I took it from the Unity manual considering POST requests here. But, there is one problem - the request never finished correctly, it always has error (which is shown using UpdateNetworkLog function): "Generic/unknown HTTP error", while request's response code is 0. Also, I've tried to use legacy method from this manual and WWW class getting the same result.
I think, problem is in how Unity gets the response: I have checked packets via Wireshark, both my POST request and server's response are perfectly fine, no errors or mistakes.
Another important issue is that I'm using WebGL, and everything's all right in Editor, program gets the proper response and works correctly; error appears only after I build and run the game in the browser (tested in Chrome and Safari). Also, tried launch it using not the Unity WebGL Player, but XAMPP: the same.
Thanks for all the future responses, as Google does not know anything about it.
From the Unity documentation on using UnityWebRequest
with WebGL:
The WWW and UnityWebRequest classes are supported in WebGL. They are implemented using the XMLHttpRequest class in JavaScript, using the browser to handle WWW requests. This imposes some security restrictions on accessing cross-domain resources. Basically any WWW request to a server which is different from the server hosting the WebGL content needs to be authorized by the server you are trying to access. To access cross-domain WWW resources in WebGL, the server you are trying to access needs to authorize this using CORS.
My best guess is that this server that you are trying to access does not have CORS correctly set up or configured. When your code is running in a browser, the browser itself is handling the request and consequently subjugating your code to that browser's security restrictions. Your program and the server aren't on the same domain, and the browser doesn't like that.
If this is your problem, then without more information it's difficult for me to suggest a solution. From your post, it sounds like you might be relatively new to coding (or at least to HTTP-related coding), and the best solutions I can find to your problem involve some relatively complex methods, such as using JavaScript instead of UnityWebRequest
.
Of course, the actual problem might even lie with your surrounding code instead, or with the server you're trying to access. What is your code for the function that calls Post()
? There could be some issue with order-of-events that's showing up only when the code is run in a browser (where JavaScript is handling the POST request).
On a side note, if the server you're trying to access has documentation for their API, your problem might be addressed somewhere in it.