UnityWebRequest Get api call returns connection error

2.2k Views Asked by At

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

1

There are 1 best solutions below

0
pixlhero On

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.