Search Unity

Unable to POST using a UnityWebRequest - but only on iOS?

Discussion in 'Scripting' started by Iseeicy, May 5, 2019.

  1. Iseeicy

    Iseeicy

    Joined:
    Jun 26, 2016
    Posts:
    5
    Hey all. I'm running into a weird bug for my recent project. I'm utilizing some google cloud stuff, and using their REST API to send compressed images as bytes.

    The code I have currently works perfectly on Android, Windows, and MacOS - but for some reason it times out on iOS! At first I thought maybe this was some kind of permissions issue, but that line of thought never led to anything... Anybody have any thoughts on what could be happening?

    This is my code

    Code (CSharp):
    1. using (UnityWebRequest www = new UnityWebRequest(restURL, "POST"))
    2.             {
    3.  
    4.                 www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonData));
    5.                 www.downloadHandler = new DownloadHandlerBuffer();
    6.                 www.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
    7.                 www.SetRequestHeader("Authorization", "Bearer " + accessToken);
    8.  
    9.                 yield return www.SendWebRequest();
    10.  
    11.  
    12.  
    13.                 if (!www.isNetworkError && www.responseCode == (long)System.Net.HttpStatusCode.OK)
    14.                 {
    15.                     //Debug.Log(www.text.Replace("\n", "").Replace(" ", ""));
    16.                     Debug.Log("Recieved response from Google!");
    17.  
    18.                     response = JsonUtility.FromJson<CustomModelResponse>(www.downloadHandler.text);
    19.  
    20.                     // SendMessage, BroadcastMessage or someting like that.
    21.                     foundResponse = true;
    22.                 }
    23.                 else
    24.                 {
    25.                     Debug.Log("Error: " + www.error);
    26.                     OnResponseFail(www.error);
    27.                 }
    28.             }
    And this is an error I get specifically in iOS


    Code (CSharp):
    1. 2019-05-05 00:20:47.721992-0500 arfortextiles[3848:320021] Task <9483C005-A87B-427A-A675-F99B8F24341F>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x2808a67c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <9483C005-A87B-427A-A675-F99B8F24341F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    2.     "LocalUploadTask <9483C005-A87B-427A-A675-F99B8F24341F>.<1>"
    3. ), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=THEURLI'MUSING, NSErrorFailingURLKey=THEURLI'MUSING, _kCFStreamErrorDomainKey=4} [-1001]
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    There is a known bug (fix in progress) where it can fail when sent data is over 2k in size and server uses HTTP/2, could this be your case?
    You can deal with this issue your self by modifying UnityWebRequest.mm and increasing the stream size. Or you can comment it out and uncomment old backend in WWWConnection.mm/h files.
     
  3. re-cheid

    re-cheid

    Joined:
    Apr 10, 2017
    Posts:
    34
    Any updates on this? Do we have a timeframe for the fix or is it already in?

    Your quick fix helps for local builds, but we want to build and sign with Unity Cloud Build.

    BR,
    Chris
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Yes, it's fixed and will be included in 2019.1.4 when it's out.
     
    re-cheid likes this.
  5. VGMFR

    VGMFR

    Joined:
    Nov 22, 2014
    Posts:
    8
    Hello @Aurimas-Cernius, thank you for your answer. When will 2019.1.4 be released? It's really a big issue on our side :/
     
  6. VGMFR

    VGMFR

    Joined:
    Nov 22, 2014
    Posts:
    8
    Hello @Aurimas-Cernius. This issue does not seem to be totally fixed in 2019.1.4.
    Depending on the size of the data that we send in POST, the request can work or not. We're getting a 400 Bad Request from Cloudflare.
    You could think that the issue is on our side, but in fact if we update the "streamSize" value of the generated UnityWebRequest.mm then it works at 100%. Everything was also perfectly working in Unity 2018.3. Therefore I think there is sill an issue in your new UnityWebRequest in Unity 2019.1.4.
    The "Unknown error" disappeared though.
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Could you report this as bug? I will look into it.
    Thanks.
     
  8. VGMFR

    VGMFR

    Joined:
    Nov 22, 2014
    Posts:
    8
    Thank you for answer. I just did a bug report, mentioning your name / this thread and with as much details as I could add.
     
  9. dej4n-gd

    dej4n-gd

    Joined:
    Aug 15, 2017
    Posts:
    4
    What was the exact change in the UnityWebRequest.mm that made it work?

    As I'm still able to reproduce this issue in 2019.1.7. In my test I send sequence of requests with body size around 50k. The issue happens only for the 1st request in the sequence. Where the content of that faulty request gets mixed up somewhere around 2k index, with blocks of data originating from somewhere else in the same request.
     
  10. Johste

    Johste

    Joined:
    Jul 12, 2014
    Posts:
    18
    I'm also seeing this bug when attempting to upload a ≈20k byte array from iOS using Unity 2019.1.10f1. Multiplying streamSize by 20 solved it, may this change have undesirable side effects?
     
  11. Johste

    Johste

    Joined:
    Jul 12, 2014
    Posts:
    18
    At the very first line UnityWebRequest.mm you can change the const CFIndex streamSize. For me changing 1024 to 20480 worked.

    Im also seeing this mixing of data at the 2k mark. (Before making a streamSize change to UnityWebRequest.mm), I've attached a picture showing base64 char comparison between the data I intended to send(red), and how it was recived by the server(green). An entire segment has been changed. Everything before and after this segment is identical.
     

    Attached Files:

  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Besides increase in memory usage this has no side effects. You can also fix this issue by modifying writeMoreBody to loop until all data is written to stream.
     
  13. Johste

    Johste

    Joined:
    Jul 12, 2014
    Posts:
    18
    Thanks, I can live with the extra memory usage for now. Is a more permanent fix scheduled anytime soon?
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Already fixed in 2019.2 and newer, 2019.1 is in the pipeline.
     
    Johste and DonLoquacious like this.
  15. dej4n-gd

    dej4n-gd

    Joined:
    Aug 15, 2017
    Posts:
    4
    Thanks. Though we found that synchronizing complete
    writeMoreBody
    method does the job until the official patch is out.
     
    Johste likes this.
  16. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    Hi @Aurimas-Cernius .
    I have trouble to upload png file on iOS to the server using REST api with Unity2019.1.5. I tried with Unity2019.1.10 too.
    but the result was same.
    I used exactly same code in this link.
    https://docs.unity3d.com/ScriptReference/WWWForm.html
    But uploaded png or jpg was truncated,
    So I converted bytes to base64 string and uploaded it.
    By comparing uploaded string and string before uploading, I realized it is different str.

    I downgraded Unity version to Unity2018.4.3 and built it again.
    In Unity2018, the problem was gone. image uploading works perfectly.
    I think this is Unity Bug.
    Please fix this problem.
    Most of my project needs Unity2019.
     
  17. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    @unitydevstudio , truncation means you are being hit by the same bug. The fix is in release pipeline, should be out soon.
     
  18. DuskGames

    DuskGames

    Joined:
    Apr 18, 2018
    Posts:
    1
    For anyone else running in to this, it is fixed starting with 2019.1.13
     
  19. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    I'm running into something that might be related in 2019.2 for a GET request. I keep running into 401 Unauthorized when I try to send the token as a requestheader in Unity - though the token works in Postman.
     
  20. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    post code, is jwt stored in x-header-access
     
    Last edited: Feb 22, 2020
    ina likes this.
  21. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    There is weird thing on iOS that NSURLSession (which is what we use to implement UnityWebRequest) does return proper HTTP response code for requests i doesn't like for some reason (for example GET request with body data). This can be determined by disabling network on phone and sending the same request. In such case you have no alternative but to find another way to do it.
     
  22. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    is pretty standard requirement though would you not say?
     
  23. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Regarding which part?
    Getting 4xx response code for request that wasn't event sent was unexpected to me, TBH.
     
  24. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    sir by pretty standard i mean sending get request with body params or header requests containing JWTs. U say it is failing to do this right?

    Alternative is to use post request, but sometimes this not satisfactory work around
     
  25. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    This is tangentially related for mobile iOS Unity, but does Unity support reading the url parameters returned to url scheme? (Have currently had to use a convoluted plugin method, as the default way to write a plugin seems to conflict with other plugins that also use the default override.)