0

So I am trying to do the Get method call to retrieve data from api server.The problem I am getting is that in Unity Editor, Both Get and Post method works perfectly fine, but in the webgl build only post method works whereas Get method returns 500 as error code and webrequest.error as Connection Error.Is there any workaround for this.

 public IEnumerator GetPlayerData()
{
    Debug.Log("This is Working");
    string url = "https:my api";

    Authorization playerData = new Authorization();
    playerData.walletaddress = PlayerPrefs.GetString("Account");
    playerData.table = "userdata";

    string jsonStringTrial = JsonUtility.ToJson(playerData);

    using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
    {
        Debug.Log("This is also Working");
        webRequest.method = UnityWebRequest.kHttpVerbGET;;
        webRequest.SetRequestHeader("Content-Type", "application/json");
        webRequest.SetRequestHeader("Accept", "application/json");
        webRequest.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonStringTrial));

        yield return webRequest.SendWebRequest();

        Debug.Log(jsonStringTrial);
        
        Debug.Log("This was not Working");

        Debug.Log("Data we Received : " + webRequest.downloadHandler.text); // this is always emtpy in webgl but works in editor
        if(webRequest.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.Log(webRequest.result);
            Debug.Log("Protocol Error");
            Debug.Log(webRequest.error);
            Debug.Log("Error Code" + webRequest.responseCode);
        }

        if (webRequest.result == UnityWebRequest.Result.DataProcessingError)
        {
            Debug.Log(webRequest.result);
            Debug.Log("DataProcessingError");
            Debug.Log(webRequest.error);
            Debug.Log("Error Code" + webRequest.responseCode);
        }

        if (webRequest.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log(webRequest.result);
            Debug.Log(webRequest.error);
            Debug.Log("ConnectionError");
            Debug.Log("Error Code" + webRequest.responseCode);
        }

        if (webRequest.result == UnityWebRequest.Result.Success)
        {
            Debug.Log(webRequest.result);
            Debug.Log("Error Code" + webRequest.responseCode);
        }

        if (webRequest.downloadHandler.text == "{}" || string.IsNullOrEmpty(webRequest.downloadHandler.text))
        {
            MainMenuHandler.Instance.OpenPanel(MainMenuHandler.Instance.playerLoginPanel);
            StartCoroutine(nameof(DisplayMessage));
            messageField.text = "Creating Player";
        }
        else
        {
            var newData = JsonConvert.DeserializeObject<Root>(webRequest.downloadHandler.text);
            PlayerPrefs.SetString(playerNamePrefKey, newData.Item.name);
            PlayerPrefs.SetInt("PlayerAvatar", newData.Item.character);
            StartCoroutine(GetUserBalance());
            yield return new WaitForSeconds(2f);
            //NetworkManager.Instance.Connect();
            StartCoroutine(nameof(DisplayMessage));
            messageField.text = "Getting Data";
        }
    }
}

Browser Console Log

  • Our ops guys always have to do some CORS voodoo for our deployed WebGL stuff, might be worth taking a look at. Anything in the editor log that might provide more details? – Retired Ninja Jul 28 '22 at 12:48
  • @RetiredNinja the editor log is completely clean , no errors or warnings. Just debugs to check if everything is working perfectly. though i can share the browser console if that would help. – BLACK GOKU Jul 28 '22 at 13:06
  • So log in the web server logs. Find out what error it threw – BugFinder Jul 28 '22 at 13:11
  • @BugFinder I will try that though I do not have access to the server. – BLACK GOKU Jul 28 '22 at 13:16

1 Answers1

0

The "correct" way to fix this issue is to have a look why the server is returnig a 500 Error. 500 errors mean that the problem is something on the server and not on the (Unity-)Client.

So it's not really a Unity problem but a Server problem.

pixlhero
  • 418
  • 2
  • 8