;Unitywebrequest connectionError

Hello everyone,

I am using unitywebreques on Unity. It works on my android windows and mac devices, but the connection error returns on the iphoe phone, what could be the reason? Unity version:2021.3.4f1

private IEnumerator CheckFriendNameRoutine(string friendName)
        {
          
            /*
             * Create the request
             */
        
            UnityWebRequest request = UnityWebRequest.Get($"https://*****/****/rest/****/GetUserInformationWithName?UserName={friendName}");

            //{
            /*
             * Setup request data
             */
            byte[] requestContent = System.Text.Encoding.UTF8.GetBytes(friendName);
                request.uploadHandler = new UploadHandlerRaw(requestContent);
                request.downloadHandler = new DownloadHandlerBuffer();
                request.SetRequestHeader("Content-Type", "application/json");

                /*
                 * Wait for request
                 */
                yield return request.SendWebRequest();
             
      
    

            if (request.result == UnityWebRequest.Result.Success)
                {
               
                PlayerInfo foundPlayerdata = JsonUtility.FromJson<PlayerInfo>(request.downloadHandler.text);
                Debug.LogError("ArkadasEkleme request error " + foundPlayerdata.UserName);
                    //_foundUserText.text = foundPlayerdata.Id == 0 ? "No user!" : foundPlayerdata.UserName;

                    if (foundPlayerdata.Id == 0)
                        _foundUserText.text = "No User!";
                    else
                        _foundUserText.text = foundPlayerdata.UserName;

                    _requestButton.interactable = foundPlayerdata.Id != 0;

                    if (foundPlayerdata.Id != 0)
                    {
                        _playerAvatar.SetActive(true);
                    }


                    _lastSearchInfo = foundPlayerdata.Id == 0 ? null : foundPlayerdata;

                }
                else
                {
                    Debug.LogError($"Couldnt check friend usernamet due to: {request.result}");
                }
                request.Dispose();

                _friendNameField.text = string.Empty;
            //}
        }

Check the device logs. There’s probably an exception being thrown, or else the code isn’t even running. Put in Debug.Log() calls to prove you are actually calling it.

If that does nothing then this might be some next things to consider:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

https://discussions.unity.com/t/831681/2

https://discussions.unity.com/t/837672/2

And setting up a proxy can be very helpful too, in order to compare traffic:

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

It seems like you’re referring to an issue that came with the iOS update. I found a temporary solution for it by using the POST method and retrieving my data that way. Even though I didn’t use a body in the GET method, it acted as if I did and blocked my API. Thank you for your attention and assistance.

1 Like

Those two things do not reflect your claims and contradict each other ^^. Why do you create an upload handler? You must not use a request body when you do a get request. Some servers and clients don’t care, may ignore or strip the body. However certain clients or servers may get upset when you try. The RFC specification has changed over the years and does not recommend using a GET body as at some point it was not allowed and certain implementations may already strip GET bodies, so the whole situation is unreliable and should be avoided.

So remove that line where you create that upload handler. You don’t need that “requestContent” either. You pass the name as a get parameter.

HTTP is a text based protocol and a request to https://my.server.com/path/file?SomeVar=foobar
would look something like this:

GET /path/file?SomeVar=foobar HTTP/1.0
Host: my.server.com
Some-Other-Header: headerValue

Note that the request header section is finished with an empty line. After that empty line the body would follow. So when you provide an upload handler with content, that content would be placed in the body. Like that:

GET /path/file?SomeVar=foobar HTTP/1.0
Host: my.server.com
Content-Length: 6
Content-Type: text/plain
Some-Other-Header: headerValue

foobar

That’s what you’re currently doing and that’s what’s the problem. Remove that body, it makes no sense unless you actually read the body on your server? Though in that case you should use a POST request which is expected to have a body. What that body looks like depends on your usecase. HTML form data for example is passed as an URL encoded string. So essentially quite similar to your search parameters that you appended to your URL.

A POST request looks exactly the same, but the method at the very start would be POST instead of GET. That’s all. It’s just an interpretation thing by clients / servers and convention. So don’t use a body with a GET request.