Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UnityWebRequest seems to be including the content-length header in GET requests on iOS

Discussion in 'iOS and tvOS' started by kb_z, Sep 14, 2023.

  1. kb_z

    kb_z

    Joined:
    May 14, 2021
    Posts:
    9
    Hi,

    I'm currently creating a simple script which fetches a static json file from a website.

    Code (CSharp):
    1.  
    2. public class UpdateAPI
    3. {
    4.     public virtual async Task<string> VersionCheck(string url)
    5.     {
    6.         using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
    7.         {
    8.             Debug.Log($"[Web API]: Querying API [{url}]");
    9.             await webRequest.SendWebRequest();
    10.             Debug.Log($"[Web API]: HTTP {webRequest.responseCode}");
    11.  
    12.             if (webRequest.responseCode != 200)
    13.             {
    14.                 Debug.LogError("[Web API]: There was a problem fetching version data.");
    15.                 Debug.LogError(webRequest.error);
    16.                 Debug.LogError(webRequest.downloadHandler.text);
    17.                 return null;
    18.             }
    19.             string response = webRequest.downloadHandler.text;
    20.             Debug.Log($"[Web API]: RS {response}");
    21.             return response;
    22.         }
    23.     }
    24. }
    25.  
    Invoking this against the server which hosts the json file returns 400 (Bad Request) every time I call it from an iOS device, however it returns the file if I call it from the editor or from Android

    I installed Charles on my PC and enabled SSL proxy to verify what the issue may have been and noticed that the requests from my test devices are including the "Content-Length" header, despite being GET requests with no body.

    Attempted to replicate the issue using postman and was able to get the server to return a 400 (Bad Request) just by adding the content-length header.

    As a work around I thought about deleting the content-length header, but I wasn't able to find a way to remove headers, just overwrite them with some limitations...

    Does anyone know of a way to sort this out?

    I did find an issue which may have introduced this behaviour:
    https://issuetracker.unity3d.com/is...ngth-when-uploading-data-using-the-get-method

    * Using Unity2021.3.9f, due to library dependencies in my project, It may not be feasible to update to a different major release
     
    Last edited: Sep 14, 2023
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Could try sending request to https://httpbin.org/get and show the returned text (webRequest.downloadHandler.text)?
     
  3. kb_z

    kb_z

    Joined:
    May 14, 2021
    Posts:
    9
    Here you go:
    note: redacted origin IP address and project name from the user-agent

    PC (Unity Editor):
    Code (csharp):
    1.  {
    2.   "args": {},
    3.   "headers": {
    4.     "Accept": "*/*",
    5.     "Accept-Encoding": "deflate, gzip",
    6.     "Host": "httpbin.org",
    7.     "User-Agent": "UnityPlayer/2021.3.9f1 (UnityWebRequest/1.0, libcurl/7.80.0-DEV)",
    8.     "X-Amzn-Trace-Id": "Root=1-6503a9da-59c0a7705b46535579d6d5e7",
    9.     "X-Unity-Version": "2021.3.9f1"
    10.   },
    11.   "origin": "***REDACTED***",
    12.   "url": "https://httpbin.org/get"
    13. }
    Android:
    Code (csharp):
    1.  
    2.  {
    3.   "args": {},
    4.   "headers": {
    5.     "Accept": "*/*",
    6.     "Accept-Encoding": "deflate, gzip",
    7.     "Host": "httpbin.org",
    8.     "User-Agent": "UnityPlayer/2021.3.9f1 (UnityWebRequest/1.0, libcurl/7.80.0-DEV)",
    9.     "X-Amzn-Trace-Id": "Root=1-6503ad9a-1c37f45d71363c906c3228db",
    10.     "X-Unity-Version": "2021.3.9f1"
    11.   },
    12.   "origin": "***REDACTED***",
    13.   "url": "https://httpbin.org/get"
    14. }
    15.  
    iOS:
    Code (csharp):
    1.  
    2. {
    3.   "args": {},
    4.   "headers": {
    5.    "Accept": "*/*",
    6.    "Accept-Encoding": "gzip, deflate, br",
    7.    "Accept-Language": "en-US,en;q=0.9",
    8.    "Content-Length": "0",
    9.    "Host": "httpbin.org",
    10.    "User-Agent": "***REDACTED***/0 CFNetwork/1402.0.8 Darwin/22.2.0",
    11.    "X-Amzn-Trace-Id": "Root=1-6503ab4c-228bf2cf1228bec122c6aadc",
    12.    "X-Unity-Version": "2021.3.9f1"
    13.   },
    14.   "origin": "***REDACTED***",
    15.  
    16.   "url": "https://httpbin.org/get"
    17. }
    18.  
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Yup, I've found it too.
    Could you submit a bug report for this and paste the issue id here, I'll try to get it on the fast track.
    Thanks.
     
  5. kb_z

    kb_z

    Joined:
    May 14, 2021
    Posts:
    9
    Not sure if I did it correctly but: IN-54957
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Thanks you, notified QA about it, hopefully they will process it quickly.