Question: I'm trying to contact a RESTful WCF POST web service from an android client which should pass some data in json format. I've already succesfully contacted a RESTful WCF GET web service, but I cannot figure out how the POST version works.

This is the android client piece of code which makes the call: 

```
HttpPost request = new HttpPost(uri);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
/*... Building the NameValuePairs object ... */
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
/* ... handling the response ...*/

```

and this is the WCF web service code:

```
[WebInvoke(Method = "POST",
        UriTemplate = "ServiceActivation",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string MyPostMethod();

public string MyPostMethod()
    {
        try
        {
            /*...*/
        }
        catch (Exception e)
        {
            /*...*/
        }
    }

```

In this way the android client succesfully contact the web service; but I don't know how to retrieve in the MyPostMethod the data passed from the android client. Something like this: MyPostMethod(string data) ends in a bad request from the android client.

So, what is the way to retrieve passed data in the web service?

Thanks in advance!

Answer: 
You have to define [data contract](http://msdn.microsoft.com/en-us/library/ms733127.aspx) for your posted data - generally class or set of related classes which will be used to deserialize JSON message and use that class as input parameter. [Here is example](http://msdn.microsoft.com/en-us/library/bb412179.aspx) how to use data contracts without WCF service - it can help you to define correct contract for your message.

Edit:

I just noticed that you are posting url encoded values - that is not a JSON request and it has different content type: `application/x-www-form-urlencoded`. JSON request for WCF is JSON passed in request content. Working with URL encoded requests in WCF is [hard](https://stackoverflow.com/questions/604463/best-way-to-support-application-x-www-form-urlencoded-post-data-with-wcf). This will change in Web API which [has support for these requests](http://wcf.codeplex.com/wikipage?title=Exposing%20a%20service%20over%20HTTP%20using%20HTTP%20Messages).
[Gson](https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Colle) makes it quite easy to build json messages in Android.

```
LoginToWebSiteRequest loginToWebSiteRequest = new LoginToWebSiteRequest();// a pojo based on some json message
loginToWebSiteRequest.setEmail(email);
loginToWebSiteRequest.setPassword(password);
Gson gson = new Gson();
String json = gson.toJson(loginToWebSiteRequest,LoginToWebSiteRequest.class);
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");

```