I’m not sure if this is a bug or intended behaviour, so thought I’d post this here to get some feedback.
I am working on a project implementing Microsoft’s Face API, which requires me to POST over HTTPS. I had some legacy code doing this using WWW, but recently converted this to use UnityWebRequest instead.
Strangely, this didn’t seem to work. After debugging with Fiddler, I realized the request was never finishing properly: it connected to the host fine, but never actually sent the POST data.
So this is where I suspect there may be a bug in UnityWebRequest (correct me if I’m wrong), particularly in the Send method; according to the documentation this method is supposed to only finish (and yes I am using it in a coroutine) when the request is done. However, after the initial SSL handshake, it continues right away, and since I am disposing of the request, the data I want to POST is never sent.
The solution is to wait until IsDone is true; which seems to defeat the purpose of Send (again, correct me if I’m wrong).
Code (including fix):
using (var request = UnityWebRequest.Put(url, image))
{
request.method = UnityWebRequest.kHttpVerbPOST;
request.SetRequestHeader("Ocp-Apim-Subscription-Key", API_KEY);
yield return request.Send();
while (!request.isDone)
yield return new WaitForEndOfFrame();
if (request.isHttpError || request.isNetworkError)
Debug.LogError(request.error);
else
{
json = request.downloadHandler.text;
Debug.Log(json);
}
}