Why does UnityWebRequest not work for me?
I am trying to send a picture to Microsofts Computer Vision API in order to get tags for the image. This works just as expected with the following solution:
private async void ComputerVisionApiRequest(byte[] byteData)
{
HttpClient client = new HttpClient();
HttpResponseMessage response;
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "mykey");
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync("https://westus.api.cognitive.microsoft.com/vision/v1.0/tag", content);
String message = await response.Content.ReadAsStringAsync();
Debug.Log("This will be a correct message:");
Debug.Log(message);
}
}
However when I am trying to use the UnityWebRequest for this, I always get an "UnsupportedMediaType"-Message as a result. Am I doing something wrong here?
IEnumerator ComputerVisionApiUnityRequest(byte[] byteData)
{
// create new POST request
UnityWebRequest www = new UnityWebRequest("https://westus.api.cognitive.microsoft.com/vision/v1.0/tag", UnityWebRequest.kHttpVerbPOST);
// create upload and download handler for the given byte array and set proper content type
www.uploadHandler = new UploadHandlerRaw(byteData);
www.uploadHandler.contentType = "application/octet-stream";
www.downloadHandler = new DownloadHandlerBuffer();
// set API key and upload and download handler to the request
www.SetRequestHeader("Ocp-Apim-Subscription-Key", "mykey");
// send the Request and wait for answer
using (www)
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("This will be a UnsupportedMediaType message:");
Debug.Log(www.downloadHandler.text);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Отправляемый файл приходит на сервер поврежденный (Uploaded file is damaged) 0 Answers
Unity Web Request fails after exporting 0 Answers
C# WebAuthenticationBroker stopped working with the new Edge update on HL2 0 Answers
WWW Class silent crach 1 Answer
C# WebAuthenticationBroker stopped working with the new Edge update on HL2 0 Answers