Reputation: 3
This is the code snippet that i have for it:
UnityWebRequest request=new UnityWebRequest(endpoint,"POST");
request.SetRequestHeader("Content-Type","application/json");
request.SetRequestHeader("host",host);
request.SetRequestHeader("X-Amz-Date",dateTime);
request.SetRequestHeader("Authorization",authorizationHeader);
request.uploadHandler=(UploadHandler)new
UploadHandlerRaw(Encoding.UTF8.GetBytes(requestParameter));
request.chunkedTransfer=false;
request.downloadHandler=new DownloadHandlerBuffer();
request.SendWebRequest();
print(request.downloadHandler.text);
Please advise as to what i am doing wrong here.....
Upvotes: 0
Views: 4803
Reputation: 139
As frankhermes says you need wait for the results to be downloaded before you do anything
yield return request.SendWebRequest();
But the above line alone didn't work for me.
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
var asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
// wherever you want to show the progress:
float progress = request.downloadProgress;
Debug.Log("Loading " + progress);
yield return null;
}
while (!request.isDone)
yield return null; // this worked for me
// after this, you will have a result
print(request.downloadHandler.text);
}
Upvotes: 0
Reputation: 4870
You need to wait for the results to be downloaded before you can do anything with it. Webrequests are asynchronous!
Typically, you would use a Coroutine to do this, like
public IEnumerator LoadData()
{
// ......
// all your code goes here, up to the SendWebRequest line
// then you yield to wait for the request to return
yield return request.SendWebRequest();
// after this, you will have a result
print(request.downloadHandler.text);
}
Start this coroutine like this:
StartCoroutine(LoadData());
More examples in the answer to this question: Sending http requests in C# with Unity
Upvotes: 3