tags:

views:

43

answers:

2

I am struggling sending data from my rest client to my rest server...

I have created a rest server sending xml to the client, and that works well. However, sending data from the client to the server, I am having a hard time.

Client:

_httpClientRead = new HttpClient("http://127.0.0.1:8000/");
var form = new HttpUrlEncodedForm();
form.Add("startDate", startDate);
_httpClientRead.Post("test", form.CreateHttpContent())

Server:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "test")]
Meeting CreateNewMeeting(string startDate);

The problem seems to be the HttpUrlEncodedForm on the client side. If I am sending an empty HttpUrlEncodedForm object in the post request, the server receives the request. When adding the the HttpUrlEncodedForm attributes, the server never receives the request, and there are no error messages!

What am I missing here? ( the server is returning xml )

How should the post data be sent to the server?

Thanks

+2  A: 

WCF expects the data to be sent serialized by the DataContractSerializer. You cannot send other media types like application/x-www-form-urlencoded by default.

See this question on how to do it. http://stackoverflow.com/questions/604463/best-way-to-support-application-x-www-form-urlencoded-post-data-with-wcf

Darrel Miller
+2  A: 

I think it is a problem that you use HttpUrlEncodedForm on the client side, while the default on the server side is Xml. To make it clear set the request format on the server side to be RequestFormat = WebMessageFormat.Xml (set this in the WebIncoke attribute). After doing this you can configure your client to send valid xml. Also make sure you use the correct xml namespace. The easiest way to handle this is to use a function that will create the content automagically for you:

var httpContent = HttpContentExtensions.CreateDataContract(objectToSendToServer);
// And then send it using post: 
_httpClient.Post("serviceUrl", httpContent); 

Note that you also need set the DefaultHeader on the HttpClient to "application/xml".

stiank81
Thanks stiank81, that fixed my problem
code-zoop