mercredi 29 juin 2016

Unity: POST request to RESTful APIs using WWW class using JSON

enter image description here

I am trying to make a POST request in Unity script. The header should contain the key 'Content-Type' that is set to 'application/json'. The input key is "email".

So here's my script:

private static readonly string POSTWishlistGetURL = "http://ift.tt/296Kz3D";
public WWW POST()
{
 WWWForm form = new WWWForm();
 form.AddField("email",  "abcdd@gmail.com");
 Dictionary<string, string> postHeader = form.headers;
 if (postHeader.ContainsKey("Content-Type"))
     postHeader["Content-Type"] = "application/json";
 else
     postHeader.Add("Content-Type", "application/json");
 WWW www = new WWW(POSTWishlistGetURL, form.data, postHeader);
 StartCoroutine(WaitForRequest(www));
 return www;
}

IEnumerator WaitForRequest(WWW data)
{
 yield return data; // Wait until the download is done
 if (data.error != null)
 {
     MainUI.ShowDebug("There was an error sending request: " + data.error);
 }
 else
 {
     MainUI.ShowDebug("WWW Request: " + data.text);
 }
}

I keep getting data.error = 400: Bad Request. How do you properly create a POST request?




Aucun commentaire:

Enregistrer un commentaire