Search Unity

UnityWebRequest.downloadHandler.text returns whole html page

Discussion in 'Scripting' started by EKO_LX, Mar 9, 2021.

  1. EKO_LX

    EKO_LX

    Joined:
    Dec 25, 2020
    Posts:
    45
    Hi,
    I'm trying to make service for registering users.

    Code (CSharp):
    1.  public IEnumerator RegisterUser(RegisterRequest register, Action callback)
    2.         {
    3.             WWWForm form = new WWWForm();
    4.             form.headers["Content-Type"] = "application/json";
    5.             form.headers["Accept"] = "application/json";
    6.  
    7.             form.AddField(nameof(register.email), register.email);
    8.             form.AddField(nameof(register.phone), register.phone);
    9.             form.AddField(nameof(register.password), register.password);
    10.             form.AddField(nameof(register.password_confirmation), register.password_confirmation);
    11.  
    12.             using (UnityWebRequest www = UnityWebRequest.Post($"{serverEndpoint}/register", form))
    13.             {
    14.                 yield return www.SendWebRequest();
    15.  
    16.                 if (www.result == UnityWebRequest.Result.ConnectionError ||
    17.                     www.result == UnityWebRequest.Result.ProtocolError)
    18.                 {
    19.                     Debug.Log(www.error);
    20.                 }
    21.                 else
    22.                 {
    23.                     Debug.Log($"Response code: {www.responseCode}");
    24.                     Debug.Log($"Response text: {www.downloadHandler.text}");
    25.                     callback();
    26.                 }
    27.             }
    28.         }
    I get response code: 200 but however as a response text I get whole html page about Laravel.
    I'm wondering the server side was set up wrong or I'm missing something in client.
    However making POST via postman returns expected result.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Maybe the server responds based on the type of received data.
    From the code it looks like you want to send data in JSON. However, you are not, you are sending an HTML form.
    If you want to send JSON, you have to manually encode your data in JSON, convert to bytes and send those via Post (don't forget to set the Content-Type, you can assign it to upload handlers property).
     
  3. EKO_LX

    EKO_LX

    Joined:
    Dec 25, 2020
    Posts:
    45
    I updated script to:

    Code (CSharp):
    1. public IEnumerator RegisterUser(RegisterRequest register, Action callback)
    2.         {
    3.             string bodyJson = JsonUtility.ToJson(register);
    4.  
    5.             using (UnityWebRequest www = UnityWebRequest.Post($"{serverEndpoint}/register", bodyJson))
    6.             {
    7.                 www.SetRequestHeader("Content-Type", "application/json");
    8.                 //www.SetRequestHeader("Accept", "application/json");
    9.  
    10.                 yield return www.SendWebRequest();
    11.  
    12.                 if (www.result == UnityWebRequest.Result.ConnectionError ||
    13.                     www.result == UnityWebRequest.Result.ProtocolError)
    14.                 {
    15.                     Debug.Log(www.error);
    16.                 }
    17.                 else
    18.                 {
    19.                     Debug.Log($"Response code: {www.responseCode}");
    20.                     Debug.Log($"Response text: {www.downloadHandler.text}");
    21.                     callback();
    22.                 }
    23.             }
    24.         }
    I'm getting same html. Also if I uncomment the line
    www.SetRequestHeader("Accept", "application/json"); 
    I get HTTP/1.1 422 Unprocessable Entity.
     
  4. EKO_LX

    EKO_LX

    Joined:
    Dec 25, 2020
    Posts:
    45
    I tried like following:
    Code (CSharp):
    1.  public IEnumerator RegisterUser2(RegisterRequest register, Action callback)
    2.         {
    3.             Debug.Log("RegisterUser2");
    4.             using UnityWebRequest request = new UnityWebRequest($"{serverEndpoint}/register", "POST");
    5.             string bodyJson = JsonUtility.ToJson(register);
    6.             byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJson);
    7.             request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    8.             request.downloadHandler = new DownloadHandlerBuffer();
    9.             //request.SetRequestHeader("Accept", "application/json");
    10.             request.SetRequestHeader("Content-Type", "application/json");
    11.  
    12.             yield return request.SendWebRequest();
    13.  
    14.             if (request.result == UnityWebRequest.Result.ConnectionError ||
    15.                     request.result == UnityWebRequest.Result.ProtocolError)
    16.             {
    17.                 Debug.Log(request.error);
    18.             }
    19.             else
    20.             {
    21.                 Debug.Log($"Response code: {request.responseCode}");
    22.                 Debug.Log($"Response text: {request.downloadHandler.text}");
    23.                 callback();
    24.             }
    25.         }
    same issue.
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Setup some HTTP debugging proxy (like Fiddler, Charles proxy) and inspect the raw requests.
     
  6. EKO_LX

    EKO_LX

    Joined:
    Dec 25, 2020
    Posts:
    45
    Thanks.
    UnityWebRequest returns error HTTP/1.1 422 Unprocessable Entity without description.
    Using Fiddler I saw actual error which was validation issue.
     
  7. praiseprof

    praiseprof

    Joined:
    May 13, 2020
    Posts:
    1
    hello, how did you fix this
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,002
    It's essentially a server side issue. So there's little you can do on the Unity side. There are countless of reasons why the server responds the way he did. This may be the result of a faulty request, that is true. However it's still a server side issue when such errors are not handled properly on the server side.

    Next time, please create your own thread since you now want to discuss your specific problem and not the problem the OP had. Chances are very very low that your issue is cause by the same thing. So unless you are here to solve the OPs issue, you should not try to hijack this thread for your own issue. Create a seperate thread and add your own details over there.