- Home /
WWW to POST data to WCF Rest Service
I have a WCF REST service made with the rest starter kit. Using WWWForm I get no error from the www.error but my server is responding that the parametrized query was not correctly formed (missing parameters)
In Fiddler the following works as a POST (but I have to specify the Content-Length header):
http://servername.com/Players/AddPlayer?name=Pedro&email=pedro@pedro.com&phone=1231231234
Here is the code that gives me the above parameter missing issue:
string url = "http://servername.com/Players/AddPlayer";
WWWForm form = new WWWForm();
form.AddField("name", "value1");
form.AddField("email", "value2");
form.AddField("phone", "value2");
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
IEnumerator WaitForRequest(WWW www)
{
yield return www;
//
if(www.error == null)
{
Debug.Log("WWW OK: " + www.text + www.error);
}
else
{
Debug.Log("WWW NO: " + www.error);
}
}
if I change the url to be the full url (`http://servername.com/Players/AddPlayer?name=Pedro&email=pedro@pedro.com☎=1231231234`) then I get a response 400 bad request, probably because it is sending as a GET and not a POST. I've been at this for hours and can't figure out what the problem is... Thanks in advance for any help!
Answer by rutter · Jun 16, 2014 at 06:37 PM
You're passing the data in two ways. The difference is subtle but crucial.
This passes your data in the query string:
http://servername.com/Players/AddPlayer?name=Pedro&email=pedro@pedro.com&phone=1231231234
This passes your data in form fields:
WWWForm form = new WWWForm();
form.AddField("name", "value1");
form.AddField("email", "value2");
form.AddField("phone", "value2");
Both options are a set of key-value pairs, but many web applications handle them separately. For example, here's a quick discussion explaining how PHP handles the two.
Your answer
Follow this Question
Related Questions
How to solve NotImplementedException during using WCF Duplex net.tcp binding? 2 Answers
Is System.Net.HttpWebRequest supported on Android/iOS 1 Answer
WWWForm and asp.net 1 Answer
Using a class library in Unity [Free version] 1 Answer
Image upload API works with postman but not working with Unity. -1 Answers