Search Unity

UnityWebRequest.Post Returning Empty String

Discussion in 'Multiplayer' started by domdev, Feb 20, 2019.

  1. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    I'm pining a Rasa server through Ngrok and postman is working fine..
    it was working in few test, but then like 3rd time test.. the return text will be empty
    any possible reason?

    I'm using Unity 2018.0.3.f2
    Windows 10

    I'm building it for android and Ios (unity cloud build)



    here my code in posting
    Code (CSharp):
    1.   IEnumerator PostRequest_(string url, string json)
    2.   {
    3.     Debug.Log(url);
    4.     var uwr = new UnityWebRequest(url, "POST");
    5.     byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
    6.     uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
    7.     uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    8.     uwr.SetRequestHeader("Content-Type", "application/json");
    9.     uwr.SetRequestHeader("Cache-Control", "no-cache");
    10.     uwr.SetRequestHeader("Pragma", "no-cache");
    11.     uwr.SetRequestHeader("Accept", "*/*");
    12.     uwr.SetRequestHeader("Accept-Encoding", "gzip, deflate");
    13.     yield return uwr.SendWebRequest();
    14.  
    15.     if (uwr.isNetworkError)
    16.     {
    17.       Debug.Log("Error While Sending: " + uwr.error);
    18.     }
    19.     else
    20.     {
    21.       Debug.Log("Received: " + uwr.downloadHandler.text);
    22.       itemData = JsonMapper.ToObject(uwr.downloadHandler.text);
    23.       bool   hasElement = false;
    24.       foreach (var element in itemData)
    25.       {
    26.         hasElement = true;
    27.         break;
    28.       }
    29.    
    30.       if (hasElement){
    31.         TTSObj.OnInputText(itemData [0]["text"].ToString());
    32.       }else {
    33.         Debug.Log("Received: Empty");
    34.       }
    35.     }
     
    Last edited: Feb 21, 2019
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
  3. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
  4. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Remove this: uwr.SetRequestHeader("Accept-Encoding", "gzip, deflate");

    On Windows we don't support gzip and this header is explicitly documented as the one to better not set manually.
     
  6. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    I'm building it for ios and android
     
  7. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    anyone?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Setting the Accept-Encoding header is not recommended on all platforms, just on Windows I know for sure you are harming yourself by putting gzip there.
     
  9. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
  10. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    I havent tested this but not sure if this will work.. cause in first test.. I only have
    uwr.SetRequestHeader("Content-Type", "application/json");
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Use some HtTP debugging proxy, like Fiddler, to inspect the actual network traffic, that usually reveals the problems.
    Also, instead of using text property on download handler use data property and see how many bytes you have received. It can be that you received a zipped response that wasn't unzipped automatically (because platform doesn't support it) and when the zipped bytes were converted to string, it resulted in something that looks empty when printed.
     
  12. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    You're using a lot of UnityWebRequest features all at once, and I don't think you need to. Just make a plain post without specifying a bunch of headers or a download handler. Also, don't deserialize, just print the text first to see that it's doing what you think it's doing.

    Your deserialization is a little fishy because itemData appears to be an array. An array primitive isn't a valid JSON type, so it's unlikely Rasa is giving you arrays.

    Clearly there's some confusion with how your server is working, especially if it works a few times and then stops working.
     
  13. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    so possible it could be my deserialization? im using litjson