Search Unity

Unity web request acting stupid.

Discussion in 'Scripting' started by Fengist, Apr 20, 2021.

  1. Fengist

    Fengist

    Joined:
    Oct 3, 2017
    Posts:
    5
    Ok, below is the code and the utterly stupid way it's working.

    I send a web request to an API on my server. It's technically a non-existent php file and it's handled by a WordPress plugin which responds with a JSON.

    If the request.downloadHandler.text response looks like this:

    {"status":"ok","Login":"Valid","ViewEula":true,"SI":"7036ac55b1a6aa62391cdf1b5ab567c9"}

    Everything works as intended. However, if the request.downloadHandler.text looks like this:

    {"status":"error","error":"Invalid username or password."}

    Web request detects that as a request.isHttpError and throws a 404 error:

    HTTP/1.1 404 Not Found

    I'm having to throw in another check when it detects that 404 to tell it, look if it really is a json response then it's not a 404 error.

    Here's the code I should be able to use.

    Code (CSharp):
    1.     public IEnumerator MyGetRequest(string url, WWWForm formData, Action<bool> callback)
    2.     {
    3.         using (UnityWebRequest request = UnityWebRequest.Post(url, formData))
    4.         {
    5.             // Send the request and wait for a response
    6.             yield return request.SendWebRequest();
    7.             Debug.Log(request.downloadHandler.text);
    8.             if (request.isNetworkError || request.isHttpError)
    9.             {
    10.                 Debug.Log(request.error);
    11.                 callback(false);
    12.             }
    13.             else
    14.             {
    15.                 if (IsValidJson(request.downloadHandler.text))
    16.                 {
    17.                     thischeck = FromJSON(request.downloadHandler.text);
    18.                     callback(true);
    19.                 }
    20.                 else
    21.                 {
    22.                     callback(false);
    23.                 }
    24.             }
    25.         }
    26.     }
    Instead, I'm having to double check to make sure the error it's detecting really is an error:

    Code (CSharp):
    1.     public IEnumerator MyGetRequest(string url, WWWForm formData, Action<bool> callback)
    2.     {
    3.         using (UnityWebRequest request = UnityWebRequest.Post(url, formData))
    4.         {
    5.             // Send the request and wait for a response
    6.             yield return request.SendWebRequest();
    7.             if (request.isNetworkError || request.isHttpError)
    8.             {
    9.                 if (!IsValidJson(request.downloadHandler.text))
    10.                 {
    11.                     callback(false);
    12.                 }
    13.                 else
    14.                 {
    15.                     thischeck = FromJSON(request.downloadHandler.text);
    16.                     callback(true);
    17.                 }
    18.             }
    19.             else
    20.             {
    21.                 if (IsValidJson(request.downloadHandler.text))
    22.                 {
    23.                     thischeck = FromJSON(request.downloadHandler.text);
    24.                     callback(true);
    25.                 }
    26.                 else
    27.                 {
    28.                     callback(false);
    29.                 }
    30.             }
    31.         }
    32.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Always get network stuff working outside of Unity in either curl or PostMan, then connect a proxy such as Charles and start trying to replicate the traffic shape with UnityWebRequest.

    Any other way risks wasting a lot of your time for trivial issues that only the above way will instantly reveal.
     
    The_Wb and Joe-Censored like this.
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    There is no stupid behavior here. isNetworkError tells you that cvomminication with the server has failed, while isHttpError means the server responded with HTTP code that means error (4xx or 5xx).
    When you get a network error, the download handler will have no or only partial data, so you can pretty much not validate JSON there.
    When you get HTTP error, the download handler contains a server sent message detailing what was the error.

    From the example you gave, the server actually sends you a questionable response in the case where you get an error. The 404 generally should be returned when requested thing is not found, there is a different error code for authorization issues, usually 403, see: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
     
    Kurt-Dekker likes this.