Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug HTTP error 500 in WebGL build while fetching localhost URL using Unity 2021 UnityWebRequest

Discussion in 'Editor & General Support' started by Antivortex, Oct 3, 2022.

  1. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    Hello.

    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.

    1) Error code is 500
    2) WebGL client is using UnityWebRequest to fetch a file from localhost.
    3) After request fails webRequest.result field has value Connection Error, webRequest.error filed has value Unknown Error
    4) Example of URL used: https://127.0.0.1:8000/@/artifacts/...mQyeu5ZjFzJFZ2bz4zdYX8yjRFS1QqCg4CF67XuQc31Uw
    5) This http error does not appear in the Chrome's Developer tools Network tab.
    6) 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.
    7) If URL is opened in the browser, the file downloads without any issues, server runs without issues.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    500 means server side error, so you need to examine server logs.
     
  3. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    Aurimas-Cernius

    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.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    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.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    would @ character in url cause any issues?
    what server you are using?
     
  6. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    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.
     
    aer0ace likes this.
  7. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    Aurimas-Cernius

    Additional information:

    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.
     
    Last edited: Oct 4, 2022
    aer0ace likes this.
  8. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    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 https://greggman.github.io/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).
     

    Attached Files:

    Last edited: Oct 4, 2022
    aer0ace likes this.
  9. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    This is the code I used for standalone project web request:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. public class Test : MonoBehaviour
    8. {
    9.     private string url = "http://127.0.0.1:8000/@/artifacts/loader/empty-scenes/contents/QmPMDiyccNRLXQgHeyFi6tibXm8kP3A6DcTjq2bSsnN8D7";
    10.     private UnityWebRequest webRequest;
    11.  
    12.     [SerializeField] private TextMeshProUGUI _text;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         webRequest = UnityWebRequest.Get(url);
    18.         webRequest.SendWebRequest();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (webRequest != null)
    25.         {
    26.             if (webRequest.isDone || string.IsNullOrEmpty(webRequest.error) == false)
    27.             {
    28.                 var lastResult = $"{webRequest.result} {webRequest.error} -" +
    29.                                  $" {url} - responseCode: {webRequest.responseCode}" +
    30.                                  $" operation isDone={webRequest.isDone}";
    31.  
    32.                 _text.text = webRequest.responseCode.ToString();
    33.            
    34.                 Debug.Log(lastResult);
    35.  
    36.                 webRequest = null;
    37.             }
    38.         }
    39.         else
    40.         {
    41.             if (Input.GetKeyDown(KeyCode.F))
    42.             {
    43.                 webRequest = UnityWebRequest.Get(url);
    44.                 webRequest.SendWebRequest();
    45.             }
    46.         }
    47.     }
    48. }
    49.  
    This is the log I got in the Unity2021-WebGL build:

    ConnectionError Unknown Error - http://127.0.0.1:8000/@/artifacts/loader/empty-scenes/contents/QmPMDiyccNRLXQgHeyFi6tibXm8kP3A6DcTjq2bSsnN8D7 - responseCode: 500 operation isDone=True


    This is the log I got in the Unity2020-WebGL build:

    Success  - http://127.0.0.1:8000/@/artifacts/loader/empty-scenes/contents/QmPMDiyccNRLXQgHeyFi6tibXm8kP3A6DcTjq2bSsnN8D7 - responseCode: 200 operation isDone=True
     
    aer0ace likes this.
  10. Antivortex

    Antivortex

    Joined:
    Sep 26, 2015
    Posts:
    7
    Problem solved, there was a local issue related to the way we link WebGL build with Javascript application
     
    tonydgwoop likes this.
  11. tonydgwoop

    tonydgwoop

    Joined:
    Jun 15, 2022
    Posts:
    1
    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.
     
  12. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    @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:
    https://forum.unity.com/threads/uni...-but-not-2020-lts-webgl.1359248/#post-8574416

    EDIT:
    Other notes on this: The Unity WebGL build is part of an Angular project.
     
    Last edited: Nov 10, 2022
  13. TimGS

    TimGS

    Joined:
    Apr 24, 2014
    Posts:
    70
    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.