Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved UnityWebRequest.SendWebRequest() is resending the previous request

Discussion in 'Scripting' started by DiscoJustice, Dec 21, 2020.

  1. DiscoJustice

    DiscoJustice

    Joined:
    Aug 21, 2013
    Posts:
    15
    I have a coroutine that I'm running in response to a button click in the editor. I'm posting data to a web server, so I make two UnityWebRequests:
    1. GET the CSRF value
    2. POST my data including the CSRF value
    I have this kind of flow working fine when running in the game itself, but this is a tool I'm running in the editor and I'm getting some strange results. Request 1 happens fine, but then when I make request 2, as soon as SendWebRequest is finished, my www object has become that of the previous request. The URL, response, etc are all wrong. Here's my code below. Look at lines 27 & 33 specifically - you can see the output of these in the log below the code. On the server side, it's as if I'm just making the first call twice, no trace of the second one.

    Code (CSharp):
    1.  
    2. IEnumerator SimplePostRequest(string url, WebUserEventArgs requestData)
    3.         {
    4.             // CSRF
    5.             Debug.Log("GET (CSRF): " + siteUrl + "/" + getCsrfUrl);
    6.             UnityWebRequest www = UnityWebRequest.Get(
    7.                       siteUrl + "/" + getCsrfUrl
    8.             );
    9.  
    10.             yield return www.SendWebRequest();
    11.             if (www.isNetworkError || www.isHttpError)
    12.             {
    13.                 Debug.LogError(www.error);
    14.             }
    15.             else
    16.             {
    17.                 csrf_token = www.downloadHandler.text;
    18.                 Debug.Log("CSRF set");
    19.             }
    20.  
    21.             Debug.Log("CSRF request was to: " + www.url);
    22.             www.Dispose();
    23.             // END CSRF CALL
    24.  
    25.             requestData.data.Add("_token", csrf_token);
    26.  
    27.             Debug.Log("Calling: "+siteUrl + "/" + url);
    28.             UnityWebRequest www2 = UnityWebRequest.Post(
    29.                       siteUrl + "/" + url, requestData.data
    30.             );
    31.  
    32.             yield return www2.SendWebRequest();
    33.             Debug.Log("(Call complete to: " + www2.url + ")");
    34.  
    35.             if (www2.isNetworkError || www2.isHttpError)
    36.             {
    37.                 Debug.LogError(www2.error);
    38.                 Debug.Log(www2.downloadHandler.text);
    39.                 requestData.fail();
    40.             }
    41.             else
    42.             {
    43.                 Debug.Log("Response: "+www2.downloadHandler.text);
    44.                 Debug.Log("(Request was to: " + www2.url + ")");
    45.                 requestData.success(www2.downloadHandler.text);
    46.             }
    47.  
    48.             www2.Dispose();
    49.         }

    And here's the output I get.

    GET (CSRF): http://[URL]/getcsrf
    UnityEngine.Debug:Log(Object)

    CSRF set
    UnityEngine.Debug:Log(Object)

    CSRF request was to: http://[URL]/getcsrf
    UnityEngine.Debug:Log(Object)

    ---Note: Line 27--- Calling: http://[URL]/swp/uploaddata/swap
    UnityEngine.Debug:Log(Object)

    ---Note: Line 33--- (Call complete to: http://[URL]/getcsrf)
    UnityEngine.Debug:Log(Object)


    I added in the "Dispose" calls in case something wasn't getting cleaned up, but no difference.

    Am I doing something wrong here?

    Unity version is 2019.4.13.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,749
    Could it be that your second request gets redirected to the same URL as the first one?
    Try setting redirect limit to zero and check the response code and Location header on second request.
     
  3. DiscoJustice

    DiscoJustice

    Joined:
    Aug 21, 2013
    Posts:
    15
    You nailed it, thank you. I still don't understand why it was being redirected to the CSRF request, but turning off redirects helped me track down the root cause which was in my web app.

    Thanks again!