Montacer Dkhilali
Montacer Dkhilali

Reputation: 399

Cannot get data from HTTP Response using UnityWebRequest in Unity

Hey Unity and Microsoft PROs! I am trying to use the Microsoft Bing Text to Speech API in Unity, this API requires an AccessToken that will be passed in the request header. to get this token, I have sent my Authentication Key in the "Ocp-Apim-Subscription-Key" header, and the the API will return back the access token that i can use next, this is a test of the API that retuns the Token in Postman.

Testing the access token via Postman

So this is the code to do this, but it doesn't 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);
    }
}

The result of this code :

enter image description here

I have already tried the WWW class, but this didn't work! and the System.Net.Http, but Unity wont accept this Library :/

Is there any way to do that, please?

Upvotes: 2

Views: 5377

Answers (2)

sonnyb
sonnyb

Reputation: 3514

I think you need a www.SendWebRequest() statement. Your code just states yield return www;, rather than yield return www.SendWebRequest().

See this code example (from 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, regarding the Access token: System.Byte[] output message with your code, note that the DownloadHandler.text property should be used rather than DownloadHandler.data for debugging the output. Currently, it is just printing the type of the property rather than its actual contents.)

EDIT: Note that I debugged the issue this way because www.isDone is false and www.downloadProgress is -1. This hints that the www request never sent or finished correctly. If it was an error, I think www.isDone would have likely been true with errors provided elsewhere.

Upvotes: 3

Anirudha Gupta
Anirudha Gupta

Reputation: 9289

Try with adding these in your request

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

and check if it's working.

Upvotes: 1

Related Questions