- Home /
Pass Headers and Arguments in UnityWebRequest POST Method Object
How to pass header data and arguments to custom server using web services created??
I knew about web service call using POST method but how to add both arguments and headers in web service call that I can't able to solve.
Up to now I have following code:
IEnumerator LoadLoginInfromation ()
{
WWWForm form = new WWWForm ();
form.AddField ("username", "admin");
form.AddField ("password", "Admin123#");
UnityWebRequest www = UnityWebRequest.Post (GameConstants.CONTESTANT_LIST_BASE_URL, form);
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log ("Data: " + www.downloadHandler.text);
}
}
Please give some help in this.
Answer by scarffy · Dec 07, 2017 at 07:55 AM
You can use SetRequestHeader instead of wwwform.
IEnumerator LoadLoginInfromation (){
www.SetRequestHeader("username", "admin");
www.SetRequestHeader("password", "Admin123#");
UnityWebRequest www = UnityWebRequest.Post(GameConstants.CONTESTANT_LIST_BASE_URL);
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log ("Data: " + www.downloadHandler.text);
}
}
Uhm it seems you don't really understand what request headers are, do you? username and password would be encoded in the request body, usually url encoded. That's what the WWWForm class actually does for you.
Also when you actually want to add request headers to your request you have to add them after you created the request but before you send it off. Currently you try to add headers before you actually create the request which makes no sense.
Your answer
Follow this Question
Related Questions
How to use the new IMultipartFormSection in a UnityWebRequest.Post 2 Answers
Pass Header Data in UnityWebRequest 2 Answers
Handle Blank Response From Web Server 0 Answers
POST request using WWW class. error: necessary data rewind wasn't possible 2 Answers
WWWForm speed different between Android and iOS device 1 Answer