- Home /
Question by
siddharth3322 · Jun 16, 2016 at 08:17 AM ·
unity 5serverwwwwebrequestwwwform
Data pass using POST method - WebRequest object
I want to use POST method to pass data over web server. I want to retrieve data provided by web server in JSON format. I have used following code by referring Unity Manual.
void Start ()
{
StartCoroutine (LoadLoginInfo ());
}
IEnumerator LoadLoginInfo ()
{
Debug.Log("Load Login Info");
WWWForm form = new WWWForm ();
form.AddField ("username", "admin");
form.AddField ("password", "Admin123#");
UnityWebRequest www = UnityWebRequest.Post (url, form);
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log ("Login Data Retrieved");
}
}
But after certain amount of wait, I am getting following message in console.
I have already checked this thing into browser but at their everything working normal.
So what is the correct way to use POST method using WebRequest object?
screen-shot-2016-06-16-at-20401-pm.png
(16.7 kB)
screen-shot-2016-06-16-at-20044-pm.png
(60.5 kB)
Comment
Answer by disny1234 · Aug 18, 2017 at 09:02 PM
Add Debug.Log(request.downloadHandler.text); See code below
void Start() { StartCoroutine(Post()); }
IEnumerator Post()
{
WWWForm wwwf = new WWWForm ();
wwwf.AddField("object_or_array", "object");
wwwf.AddField("empty", "false");
wwwf.AddField("parse_time_nanoseconds", "78983");
wwwf.AddField("validate", "true");
wwwf.AddField("size", "1");
var request = UnityWebRequest.Post("http://validate.jsontest.com/?json=%7B%22key%22:%22value%22%7D", wwwf);
yield return request.Send();
Debug.Log(request.downloadHandler.text);
}
response output { "object_or_array": "object", "empty": false, "parse_time_nanoseconds": 46600, "validate": true, "size": 1 }