POST request to RESTful APIs
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://mongodb-serverURL.com/api/WishlistGet";
public WWW GET()
{
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?
wishlistgetscreenshot.png
(56.6 kB)
Comment
Your answer
Follow this Question
Related Questions
Having Trouble with Post to Server 0 Answers
[WEBGL] Post json - complicated structure 0 Answers
Why does UnityWebRequest not work for me? 0 Answers
WWW WP8 Cache? Maybe requests limit? 0 Answers