Unable to get data from HTTP response using UnityWebRequest in Unity

Hi Unity and Microsoft PRO! I am trying to use the Microsoft Bing Text to Speech API in Unity , this API requires an AccessToken , which will be passed in the request header. to get this token, I sent my authentication key to the β€œOcp-Apim-Subscription-Key” header, and the API will return an access token, which I can use next, is an API test that reconfigures the token in Postman.

Testing the access token through the mail server

So, this is the code for this, but it does not work.

using System.Collections; using System.Collections.Generic; using System.Xml.Linq; using UnityEngine; using UnityEngine.Networking; public class Test : MonoBehaviour { public static readonly string accessUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; public string accessToken; public void Start() { WWWForm wwwForm = new WWWForm(); Dictionary<string, string> headers = wwwForm.headers; headers["Ocp-Apim-Subscription-Key"] = "a66ec1e2123784hf39f22e2dc2e760d13x"; UnityWebRequest www = UnityWebRequest.Post(accessUri, wwwForm); StartCoroutine(RequestToken(www)); } public IEnumerator RequestToken(UnityWebRequest www) { yield return www; if (www.error == null) { Debug.Log("downloadedBytes : " + www.downloadedBytes); Debug.Log("certificateHandler : " + www.certificateHandler); Debug.Log("chunkedTransfer : " + www.chunkedTransfer); Debug.Log("downloadHandler : " + www.downloadHandler); Debug.Log("downloadProgress : " + www.downloadProgress); Debug.Log("isDone : " + www.isDone); Debug.Log("isNetworkError : " + www.isNetworkError); Debug.Log("method : " + www.method); Debug.Log("redirectLimit : " + www.redirectLimit); Debug.Log("responseCode : " + www.responseCode); Debug.Log("uploadedBytes : " + www.uploadedBytes); Debug.Log("useHttpContinue : " + www.useHttpContinue); } else { Debug.Log("Error" + www.error); } var p = www.downloadHandler.data; Debug.Log("Access token: " + p); } } 

Result of this code:

enter image description here

I already tried the WWW class , but it did not work! and System.Net.Http , but Unity will not accept this library: /

Is there a way to do this, please?

+5
source share
2 answers

I think you need the operator www.SendWebRequest() . Your code simply indicates yield return www; , not yield return www.SendWebRequest() .

See this sample code (from the UnityEngine.Networking.UnityWebRequest.Post Documentation ):

 using UnityEngine; using UnityEngine.Networking; using System.Collections; public class MyBehavior : MonoBehaviour { void Start() { StartCoroutine(Upload()); } IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData"); using (UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form)) { yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } } } } 

(Also, as regards the output message Access token: System.Byte[] with your code, note that instead of debugging the output, use the DownloadHandler.text property rather than DownloadHandler.data . Currently, it just prints the property type, and not its actual content.)

EDIT: Please note that I debugged the problem this way because www.isDone is false and www.downloadProgress is -1. This means that the www request was never sent or ended correctly. If this was a mistake, I think that www.isDone probably be true with errors provided elsewhere.

+2
source

Try adding them to your request.

 request.Accept = @"application/json;text/xml"; request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000"; 

and check if it works.

+1
source

Source: https://habr.com/ru/post/1276187/


All Articles