We are having some troubles resolving HTTP error trying to load some local files in the WebGL build made with Unity 2021.3.6f1 and bundled with a Javascript application running in the browser.
Error code is 500
WebGL client is using UnityWebRequest to fetch a file from localhost.
After request fails webRequest.result field has value Connection Error, webRequest.error filed has value Unknown Error
This http error does not appear in the Chrome’s Developer tools Network tab.
This error does not happen if the build is made with Unity 2020.3.34f1 with the same project and Javascript server running, for Unity 2020 WebGL client gets correct response and downloads the file.
If URL is opened in the browser, the file downloads without any issues, server runs without issues.
So far we think that the request does not reach the server since the response with code 500 does not appear in the Chrome web browser Developer Tools Network tab. Server logs do not show it as well.
One thing to check here is the URL to your application and URL you are requesting.
By default in WebGL you can only do requests to your application (I think that include the path too, not only the host). To access other stuff, you have to setup CORS.
We have a confirmation it is caused by something on WebGL side.
We made an empty Unity project with simple script doing this request, response is 200 in the Windows Standalone build but 500 in the WebGL build. We will try to see what we can do about CORS on the server.
Exactly same project build for WebGL in Unity 2020 receives correct response 200 from the same server. But if built with Unity 2021 then response is error 500.
If this is about CORS on the server, could it be different for Unity 2020 and Unity 2021?
UPD:
Also response 200 in 2020 case appears in the Chrome browser developer tools but for response 500 it does not appear completely which makes us conclude that the request does not leave Unity altogether.
These are the response headers coming from our server (screenshot attached) if exactly same GET request being done with Postman. Seems like Access-Control-Allow-Origin is * which means the CORS is enabled, right?
I tried to run a different web server (Servez Servez | servez ) which has CORS policy flag in its configuration. I simulated exactly same localhost URL and tried to do the request with a clean WebGL build. Results are these:
(1) If CORS flag is on in Servez for Unity2021-WebGL build as a client then I see response code 200.
(2) If CORS flag is off in Servez for Unity2021-WebGL build as a client then I see response code 500.
(3) If CORS flag is on in Servez for Unity2020-WebGL build as a client then I see response code 200.
(4) If CORS flag is off in Servez for Unity2020-WebGL build as a client then I see response code 200.
So 500 comes only for combination of CORS flag off and Unity2021-WebGL.
The only difference in the response headers for case (2) and (1) is Access-Control-Allow-Origin: * key-value pair missing in the response headers. But this key-value pair is present in the response headers of our custom server (all headers you can see in the screenshot) though for our custom server Unity2021-WebGL shows 500 and Unity2020-WebGL shows 200.
And again: response 500 does not appear in the browser for case (2) but 200 appears in the browser for case (1).
This is the code I used for standalone project web request:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
public class Test : MonoBehaviour
{
private string url = "http://127.0.0.1:8000/@/artifacts/loader/empty-scenes/contents/QmPMDiyccNRLXQgHeyFi6tibXm8kP3A6DcTjq2bSsnN8D7";
private UnityWebRequest webRequest;
[SerializeField] private TextMeshProUGUI _text;
// Start is called before the first frame update
void Start()
{
webRequest = UnityWebRequest.Get(url);
webRequest.SendWebRequest();
}
// Update is called once per frame
void Update()
{
if (webRequest != null)
{
if (webRequest.isDone || string.IsNullOrEmpty(webRequest.error) == false)
{
var lastResult = $"{webRequest.result} {webRequest.error} -" +
$" {url} - responseCode: {webRequest.responseCode}" +
$" operation isDone={webRequest.isDone}";
_text.text = webRequest.responseCode.ToString();
Debug.Log(lastResult);
webRequest = null;
}
}
else
{
if (Input.GetKeyDown(KeyCode.F))
{
webRequest = UnityWebRequest.Get(url);
webRequest.SendWebRequest();
}
}
}
}
This is the log I got in the Unity2021-WebGL build:
I encountered the same issue on my website after upgrading an older Unity WebGL project to Unity 2021.
The UnityWebRequest would fail with a 500 error code response “Unknown error” even though the request never hit my API server. There were no CORS errors or network traffic visible in the chrome debug tools either, so I figured this had to be something related to the game build or the way the Unity game was loading on my website.
The fix for me was to update the {yourGameName}.loader.js file on my website with the new one that was generated in the Unity 2021 build.
I had renamed my version of that file to unity.loader.js and used it to load several different games across the site. From there, I serve game builds separately from a storage account and became used to only updating those game build files. It wasn’t entirely clear that the javascript loader would also need to be updated along with the game builds from Unity 2021.
@Antivortex I’m running into this exact same issue. Would you mind providing any details to your fix? I’m not sure if I should continue diving down the CORS rabbit hole still. I believe I have all CORS set up properly on my content server, my API server, and in my Unity build.
Here is my thread about it, but I haven’t gotten any farther:
EDIT:
Other notes on this: The Unity WebGL build is part of an Angular project.
I was getting error 500 and the problem was in my app logic.
My web request was used in a utility method and created request.uploadHandler without checking method type. So I was getting error 500 for GET requests and uploadHandler!=null
Which is expected because unitywebrequest uses js fetch function and this function return error if GET/HEAD method has a body.