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.
public IEnumerator MyGetRequest(string url, WWWForm formData, Action<bool> callback)
{
using (UnityWebRequest request = UnityWebRequest.Post(url, formData))
{
// Send the request and wait for a response
yield return request.SendWebRequest();
Debug.Log(request.downloadHandler.text);
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
callback(false);
}
else
{
if (IsValidJson(request.downloadHandler.text))
{
thischeck = FromJSON(request.downloadHandler.text);
callback(true);
}
else
{
callback(false);
}
}
}
}
Instead, I’m having to double check to make sure the error it’s detecting really is an error:
public IEnumerator MyGetRequest(string url, WWWForm formData, Action<bool> callback)
{
using (UnityWebRequest request = UnityWebRequest.Post(url, formData))
{
// Send the request and wait for a response
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
if (!IsValidJson(request.downloadHandler.text))
{
callback(false);
}
else
{
thischeck = FromJSON(request.downloadHandler.text);
callback(true);
}
}
else
{
if (IsValidJson(request.downloadHandler.text))
{
thischeck = FromJSON(request.downloadHandler.text);
callback(true);
}
else
{
callback(false);
}
}
}
}