I posted a question in scripting about webGL and calling a API. Not getting any good responses. So I figured I’d ask in here since I may be on a wild goose chase at this point. Has anyone successfully made calls to an API using WebGL? I have tried HTTP Post and now Unity Web Request and still does not work when I test locally using FireFox. Are their requirements I’m missing?
Snippet:
IEnumerator GetJsonContent(string url, string bodyJsonString)
{
var request = UnityWebRequest.Post(url, "{}");
request.method = "POST";
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
while (!request.isDone)
{
yield return new WaitForEndOfFrame();
}
if (request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
//var jsonContent = request.downloadHandler.text;
var response = request.downloadHandler.data;
string json = System.Text.Encoding.UTF8.GetString(response);
var _Response = JsonConvert.DeserializeObject<string>(json);
txtResponse.text = _Response;
}
yield break;
}