Hi all, I have a problem when using either WWW/UnityWebRequest to send JSON data to my server.
I have tested my server request API on Postman, and it works. The server sends back text msg (like “Success”). But it doesn’t work on Unity3D. I have tried both WWW and UnityWebRequest to send my JSON data. The code shows below, (I only posted UnityWebRequest one),
btw: the JSON I want to send is {“data”, AnotherJSON}, thus I AddField by AddField(“data”, jsonstr)
IEnumerator PostData(){
User usr = new User();
usr.name = "abc";
usr.email = "abc@gmail.com";
usr.age_range = 23;
usr.gender = "male";
usr.location = "U.S.";
string jsonStr = JsonConvert.SerializeObject(usr);
WWWForm form = new WWWForm();
form.AddField("data", jsonStr);
//Dictionary<string, string> headers = form.headers;
//headers["Content-Type"] = "application/json";
UnityWebRequest uwr = UnityWebRequest.Post(addUerURL, form);
uwr.SetRequestHeader("Content-Type", "application/json");
yield return uwr.Send();
string s = uwr.downloadHandler.text;
if(uwr.isError){
Debug.Log(uwr.error);
}else{
Debug.Log("Success? " + s);
}
}
The User class is just a public class contains basic info using default constructor. The output goes into “No Error” and prints “Sucess? 400 Bad Request” which indicates my request are malformed.
And when I use “WWW www” as sending my request, I got the same, 400 Bad Request.
Anyone can help me out??? A lot thanks in advance.