- Home /
Try catch alternative for HTTP request
Hello,
When connecting to a REST api, randomly and in some android devices I get errors when doing a POST. The errors are allways Curl error.... and they usually appear when the users are connected to free wifis and probably with wifis with extra security.
The issue is that when the error appear, the coroutine crashes, and fails, It doesnt return a http or network error and I am being unable to control the crashes because I cant use try catch with a return statement inside.
using (var request = new UnityWebRequest(string.Concat(server, url, applications, id, refreshToken), "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(form);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + loginInfo.data.access_token);
yield return request.SendWebRequest();
Answer by Captain_Pineapple · Jun 10, 2021 at 09:24 AM
Hey,
only thing i could think of at the top of my head would be to investigate if changing from Coroutines to an async function can be done here using something like this Webrequest Wrapper here (GitHubLink)
If i remember correctly you can add try catches around a complete async function which makes it superior in this case.
Your answer
Follow this Question
Related Questions
How to use the new IMultipartFormSection in a UnityWebRequest.Post 2 Answers
POST to HTTP not working 2 Answers
Unity HTTP Post WebRquest not working on some Android Devices 0 Answers
Why do I get Parse error (code 400) from Google Ftiness API in Unity? 0 Answers
Capturing and Tracing All HTTP Requests in Unity project 0 Answers