I’m migrating from WWW class to UnityWebRequest.
I have very simple PHPscript which always responses with get and post data you are sending to it.
I have 2 pieces of code in unity:
The First:
Dictionary postHeader = new Dictionary();
postHeader.Add("Content-Type", "application/json");
byte[] byteArray = Encoding.UTF8.GetBytes("{\"a\":0}");
WWW www = new WWW(script_url, byteArray, postHeader);
yield return www;
print(www.text);
this one prints {“message”:“ok”,“get”:“”,“post”:{“a”:0}}
And second:
var request = UnityWebRequest.Post(script_url, "{\"a\":0}");
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
print(request.downloadHandler.text);
This code is from the help and it prints:
{“message”:“ok”,“get”:“”,“post”:null}
Therefore UnityWebRequest ignores post body. My question is how do I send post data to server with UnityWebRequest?
The problem was our server relies post body is raw json string while unity sends URLEncoded string