UnityWebRequest.SendWebRequest() is resending the previous request

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:

  • GET the CSRF value
  • 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.

IEnumerator SimplePostRequest(string url, WebUserEventArgs requestData)
        {
            // CSRF
            Debug.Log("GET (CSRF): " + siteUrl + "/" + getCsrfUrl);
            UnityWebRequest www = UnityWebRequest.Get(
                      siteUrl + "/" + getCsrfUrl
            );

            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.LogError(www.error);
            }
            else
            {
                csrf_token = www.downloadHandler.text;
                Debug.Log("CSRF set");
            }

            Debug.Log("CSRF request was to: " + www.url);
            www.Dispose();
            // END CSRF CALL

            requestData.data.Add("_token", csrf_token);

            Debug.Log("Calling: "+siteUrl + "/" + url);
            UnityWebRequest www2 = UnityWebRequest.Post(
                      siteUrl + "/" + url, requestData.data
            );

            yield return www2.SendWebRequest();
            Debug.Log("(Call complete to: " + www2.url + ")");

            if (www2.isNetworkError || www2.isHttpError)
            {
                Debug.LogError(www2.error);
                Debug.Log(www2.downloadHandler.text);
                requestData.fail();
            }
            else
            {
                Debug.Log("Response: "+www2.downloadHandler.text);
                Debug.Log("(Request was to: " + www2.url + ")");
                requestData.success(www2.downloadHandler.text);
            }

            www2.Dispose();
        }

And here’s the output I get.

UnityEngine.Debug:Log(Object)

CSRF set
UnityEngine.Debug:Log(Object)

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

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

---Note: Line 33--- (Call complete to: http:///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.

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.

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!