Well, this is a common pitfall with Unity’s Post method. For some reason they throught it’s a great idea that by default any Post request will send url encoded data which is extremely unintuitive. See the exact implementation here.
If you want to post raw text / json / binary / whatever you have to provide your own UploadHandlerRaw and provide your data that way.
request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(data));
This will replace the upload handler with our own and we simply perform an UTF8 encoding on the text.
I used my SimpleJSON (since I already had it in my project) just to run this example:
async void Start()
{
JSONNode n = new JSONObject();
n["test"] = 42;
Debug.Log("Request: " + n.ToString(3));
string res = await PostData("MyURL/php/JsonTest.php", n.ToString());
var node = JSON.Parse(res);
Debug.Log("Response: " + node.ToString(3));
}
So the php script will parse the json into an object, add a new field called “result” and simply return whatever was send in to the client. The two log statements in Unity look like this:
So this works as expected once you use a raw upload handler.
Hmm, i tried what you suggested and im getting this as an error now HTTP/1.1 400 Bad Request Do you know if my web service only allowing https would cause an error?
Hmm, i tried what you suggested and im getting this as an error now HTTP/1.1 400 Bad Request Do you know if my web service only allowing https would cause an error?
– Yoconn