- Home /
What is a good way to determine if a UnityWebRequest succeeded?
The samples in the manual show using UnityWebRequest.isError method. Unfortunately (see the docs for that field) isError returns false when the web server returns an error such as 404 Not Found.
What would be a good way to determine if your web request succeeded in returning data that you can use? I'm thinking of something like this:
private bool IsFailedRequest(UnityWebRequest request)
{
return request.isError || (request.responseCode != (long)System.Net.HttpStatusCode.OK);
}
But I'm concerned that there may be other success status codes that I should be checking for. Also, although I'm getting 200 (HttpStatusCode.OK) back from a successful UnityWebRequest.Get, the result from a successful UnityWebRequest.GetAssetBundle call is returning 0 (not a status code). Is there a better way to check for success?
Answer by Forrest-Trepte · Apr 19, 2017 at 06:24 PM
I found that sometimes success is returned as HttpStatusCode.OK and sometimes as 0. So far, handling it like this seems to produce the expected results:
public static bool IsSuccess(this UnityWebRequest request)
{
if (request.isError) { return false; }
if (request.responseCode == 0) { return true; }
if (request.responseCode == (long)System.Net.HttpStatusCode.OK) { return true; }
return false;
}
Have implemented this solution in my UWR useage at work, I believe this to be current for the latest Unity version too. Rewarded/upvoted.
Answer by ickydime · Nov 02, 2017 at 08:05 PM
isError is depreciated in Unity 2017.
Check for IsHttpError for HTTP responses other than 200 (success) and check isNetworkError for system errors.
Your answer
Follow this Question
Related Questions
Adding Custom Headers/Cookies to the UnityWebRequest.Post 1 Answer
Pass Headers and Arguments in UnityWebRequest POST Method Object 1 Answer
UnityWebRequest timing out ? Send() finishes but isDone = false 1 Answer
UnityWebRequest.POST calls to an HTTPS url throws "Unknown error" on specific device 0 Answers