unitywebrequest sometimes returns null

sometimes the webrequest just returns null and yet when i click the button to do the request again it works fine…this does not happen all the time but enough that it is annoying, is there something wrong with my code??

IEnumerator webrequestcall()
    {
        WWWForm form = new WWWForm();
        form.AddField("gcid", gcid);
        UnityWebRequest www = UnityWebRequest.Post("url", form);
        yield return www.SendWebRequest();
        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log("Error contacting Server");
        }
        else
        {
           
            string returndata = www.downloadHandler.text;
            Debug.Log("aaa " + returndata);           
    }
1 Like

This is unlikely your problem, but UnityWebRequest implements the IDisposable interface, which means you should probably stick it in a using() block.

Also, try other endpoints than the one URL. Also, are you seeing any errors? If you build to device, are you seeing any exceptions such as SSL exceptions?

It’s also possible that your server doesn’t like whatever your “gcid” value is. Do you have server logs for the requests that return null? What was the server’s response in those cases?

Also, presumably “url” is just a string you’re using on the forums and not what’s actually in your code right?

yeah the addfield gcid and url are just placeholders… There are no errors in the log at all…it just returns null. it has happened when using multiple urls and as i said does not happen all the time but just some times, really not sure what the problem is and why it only happens sometimes and not all the time.

how would i put this in a using() block, never done that before? will give it a go and see if it works.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement has an explanation and example.

OK I made the change to the following code

 using (UnityWebRequest request = UnityWebRequest.Post("url", form))
        {
            yield return request.SendWebRequest();
            if (request.isNetworkError || request.isHttpError)
            {
                Debug.Log("Error contacting Server");
            }
            else
            {
                string returndata = request.downloadHandler.text;
                Debug.Log("aaa " + returndata);
              
            }
}

The same thing is still happening however what I find very interesting is it only happens if you try and send a request around the 2 second mark of the first one returning…If you click the button to send <2 seconds it works fine…if you wait for about 4 seconds or more it works fine…if you wait approx 2 seconds and send it, it Always comes back blank…

Are you calling “IEnumerator webrequestcall()” directly from the button?

Try calling a method that starts a coroutine with webrequestcall()

button is linked to a function when clicked

public void buttonclick(){
   startcoroutine(webrequestcall());
}

this behaviour is really strange btw, you should try with another entirely different post request that you know is guaranteed to work and see if it behaves the same

It seems to happen with all request, i have made multiple now and they all seem to fail at some time or another, the way i go around it was to duplicate the IEnumerator function and call it webrequestcall2, when webrequest call returns null it immediately starts coroutine for webrequestcall2 and that one works. I gave up on trying to fix the problem, absolutely no idea what is causing it, i have tried calling dispose and using the using() block. All seem to fail maybe 1 in 20 times always around the 2 second mark of the 1st coroutine returning from the server.

Does it look like request was successful but no data returned?
If so, setup HTTP debugging proxy (such as Fiddler) and examine the request/response of the failed one.
If the error is something else, paste the contents from the console.

there is no error in the logs at all, I did play around with the other values to see what was happening, if i try and debug.log the returndata the app just hangs however if you just check it is null then the app continues.

I also tried logging getresponseheaders and it received 2 headers when the webrequest succeeded but none when the value returned null. It seems almost like unity is cancelling the request before it even reaches the server and just returning a null value.

I will look into fiddler later on and see if that can show anything.

What is null?
The error you describe is a symptom of using UWR after it has been disposed, which would mean an error in your code. Can’t tell exactly without knowing what your actual code looks like exactly.

my code is below someone clicks a button and then the function starts the coroutine

 public void jobclick()
    {
            StartCoroutine(DoStuff());
    }

IEnumerator DoStuff()
{
    WWWForm form = new WWWForm();
    form.AddField("Jobtype", joby);
    UnityWebRequest www = UnityWebRequest.Post("urlhere", form);
    yield return www.SendWebRequest();
    if (www.isNetworkError || www.isHttpError) { Debug.Log("Error contacting Server"); }
    else
    {
        string returndata = www.downloadHandler.text;
        if (returndata.Length > 2)
        {
            string[] myarray = returndata.Split('|');
            if (myarray[0] == "1")
            {
               // do stuff here
            }
            else
            {
                // do error message
            }
        }
        else
        {
            Debug.Log("retyring code");
            StartCoroutine(DoJobs2());  // starts a coroutine to resend the data
        }
    }
    www.Dispose();

}
1 Like

At what point do you get null? What is the exact error in console?

no error in console at all
string returndata = www.downloadHandler.text;
returndata just returns null as in nothing there…i even added a line on the server error_log to log everytime it hits the server and when it returns null it doesn’t even reach the server. I will test later on with another url and see if the error persists but it errors so fast that it seems like it doesn’t even have time for the data to get to my server before it returns null in www.downloadHandler.text

Maybe the response just couldn’t be encoded as a string. Did you check the raw byte data from download handler?

I didnt but i did output the headers as in

UnityWebRequest.GetResponseHeaders

When www.downloadhandler.text returned null the response headers were also null as in nothing in them, when the request worked fine the response headers had a dictionary with 2 strings in them.

That sounds low. Normally there are much more headers. What is your server setup?
You certainly need to setup Fiddler or similar and examine the request and response.

1 Like