Sending post request with UnityWebRequest
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?
Answer by undefined666 · Feb 15, 2019 at 05:20 PM
The problem was our server relies post body is raw json string while unity sends URLEncoded string
Your answer
![](https://koobas.hobune.stream/wayback/20220612191746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
WWW HTTP Post request only works on Android if i'm comnected to Wifi, not on cellular data 1 Answer
WWW class and UnityWebRequest no longer working in Unity 2017.3.0 5 Answers
Unable to acess video file inside my apk from unity 0 Answers
Vuforia + Firebase + Asset Bundles : How To 2 Answers
How to change AudioClip load type, which was loaded from drive? 0 Answers