API POST call works in POSTMAN but not in Unity
Hey everyone. In my project, after a player inputs their first name, last name, and email address, an API POST call is made sending these strings after being saved in variables. I have tested this call in POSTMAN and it works fine, but in Unity it gives me a "Generic / Unknown HTTP Error."
IEnumerator AWSCall(){
WWWForm test_form = new WWWForm();
test_form.AddField("first_name", fName);
test_form.AddField("last_name", lName);
test_form.AddField("email_address", eMail);
using (var w = UnityWebRequest.Post(AWS_URL, test_form)){
yield return w.Send();
if(w.isNetworkError || w.isHttpError){
print(w.error);
}else{
print("Transfer Complete");
}
}
Anyone know what could be the issue? Thanks!
Answer by avorobjev · Jan 23, 2018 at 11:42 AM
It looks like it's a bag https://forum.unity.com/threads/post-requests-doesnt-work-in-2017-3.510281/ https://issuetracker.unity3d.com/issues/networking-unitywebrequest-dot-post-returns-generic-slash-unknown-http-error
I changed my methods from WWW to UnityWebRequest like this:
IEnumerator Post()
{
string postData = "{....post data json...}";
byte[] bytes = GetBytes(postData);
using (UnityWebRequest www = UnityWebRequest.Put("http://localhost/api/PutMethod", rawData))
{
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader ("Accept", "text/json");
yield return www.Send();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
}
Yep. I basically had to do the same thing. $$anonymous$$ade a Json file and class. The inputs from the player changed the values in the class and then used the UnityWebRequest.
I have a new issue now, wonder if anyone can help out. The POST call when successful is supposed to return 2 ID numbers in the form of a JSON. So they are like this:
{ "opp_id": "006f4000005XIN0AAO", "hiker_id": "a04f4000001dvd$$anonymous$$AAQ" }
When I try to access them with www.downloadHandler.text, sometimes I get {"error":"List has no rows for assignment to SObject"}. Other times I can see them, but then it doesn't work when I try to turn them into new strings and eventually work with them as variables. Any idea what is going on here?
Answer by montacerdk · Apr 03, 2018 at 08:13 AM
Hey guys, I am trying to invoke a Web Service in Unity, it requires first an access token that i'll get it from this API : https://api.cognitive.microsoft.com/sts/v1.0/issueToken. I order to recieve the access token, I should use a POST request with a Ocp-Apim-Subscription-Key as a header and no data will be passed in the HTTP body, it works correctly with Postman, but in Unity, i get this Error :
Error is : Generic/unknown HTTP error
UnityEngine.Debug:Log(Object)
<RequestToken>c__Iterator0:MoveNext() (at Assets/Scripts/Test.cs:26)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
This is my code :
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class Test : MonoBehaviour
{
public static readonly string accessUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
private string apiKey;
private string accessToken;
public void Doo ()
{
StartCoroutine(RequestToken());
}
public IEnumerator RequestToken()
{
UnityWebRequest request = new UnityWebRequest(accessUri, UnityWebRequest.kHttpVerbPOST);
request.SetRequestHeader("Ocp-Apim-Subscription-Key", "a66ec1e2efed47639f22e2dc2e760d13x");
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log("Error is : " + request.error);
}
else
{
Debug.Log("downloadHandler Text : " + request.downloadHandler.text);
Debug.Log("responseCode : " + request.responseCode);
Debug.Log("isDone : " + request.isDone);
Debug.Log("method : " + request.method);
}
}
}
Answer by Tarrag · Apr 23, 2020 at 10:16 PM
Hey @montacerdk did you find a solution to this? cheers!