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

Bug UnityWebRequest bug when device language is not English

Discussion in 'Scripting' started by Archieonic, Mar 8, 2023.

  1. Archieonic

    Archieonic

    Joined:
    Sep 13, 2019
    Posts:
    14
    Hello,

    So I've been using HuggingFace's API without issues making calls to it with UnityWebRequest. However, something particularly curious is happening and I can't, for the life of me, figure out the root cause of it.

    Whenever the device (Android) language is set to anything other than English, the web request fails and gives error http 400 Bad Request. The API response is that there's an unexpected character. What I can't understand is why the device language triggers this issue at all. Again, if the device's language is English, no error occurs. But if it's something else (Spanish, French, Italian, etc.) then I get that error.

    Is there something in the request that changes if the device's language is different?

    Any help is greatly appreciated.

    Code (CSharp):
    1.         var json = Json.Serialize(form);
    2.         Debug.Log("JSON" + json);
    3.         byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
    4.  
    5.         // Make the web request
    6.         UnityWebRequest request = UnityWebRequest.Put(model_url, bytes);
    7.         request.SetRequestHeader("Content-Type", "application/json");
    8.         request.SetRequestHeader("Authorization", "Bearer " + hf_api_key);
    9.         request.method = "POST";
    10.  
    11.         yield return request.SendWebRequest();
    12.  
    13.         // If the request return an error set the error on console.
    14.         if (request.isNetworkError || request.isHttpError)
    15.         {
    16.             Debug.Log(request.error);
    17.             Debug.Log(request.downloadHandler.text);
    18.             yield return request.error;
    19.         }
    20.         else
    21.         {
    22.             JSONNode data = request.downloadHandler.text;
    23.             // Process the result
    24.             yield return ProcessResult(data);
    25.         }
     
  2. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    70
    Archieonic likes this.
  3. Archieonic

    Archieonic

    Joined:
    Sep 13, 2019
    Posts:
    14
  4. Archieonic

    Archieonic

    Joined:
    Sep 13, 2019
    Posts:
    14
    It worked (different approach), in this scenario all I had to do apparently was force the app's CurrentCulture to "en-US" and that also solved the issue with JSON. Much appreciated.
     
    JuanGuzmanH likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    That's a bad solution for two reasons. First of all json and most other data transfer formats usually rely on the invariant culture. The invariant culture is currently mostly identical with the US culture. However the US culture could change in the future, the invariant culture does not. So unless you're reading in user input (which usually is expected to be in the local culture) or you just display values to the user, you should use the invariant culture whenever possible.

    What exact json framework do you use to create / parse the request / response? For example even my own SimpleJSON, had a missing a proper ToString formatting with the invariant culture in the JSONNumber class which is fixed now. So whatever framework you're using, that framework also must have made a similar mistake and should be fixed. JSON is a well defined format and it's based on the invariant culture. So make sure you use the latest version of that framework and if you can reproduce this bug in that framework, report it to that framework. Don't just hack around the issue so the next 100 user would bump into the same issue :)
     
  6. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    70
    Bunny83 is totally right. In my case my json file was a json app setting file made by me, no user input and not showing it in UI. Invariant culture seems to be the right path.