Wednesday, September 1, 2010

Using Fiddler to test WCF Post methods

By default, WCF only allows JSON and XML for post data. It'll not allow callers to post data using html forms unless mime type application/x-www-form-urlencoded is supported.

In order to post data, json or xml, view the service /help page which gives you the exact format. This message template can be used as fiddler Request Body under Request Builder Tab.

When mime type application/x-www-form-urlencoded is enabled from wcf service. past the query string in Request Body

For e.g. if your html link was like http://www.mysite.com/index.html?param1=1&param2=2&param3=3

In Fiddler Request Builder
- Set Method to Post
- url as http://www.mysite.com/index.html
- Request Header
    User-Agent: Fiddler
    content-type: application/x-www-form-urlencoded
- Request Body
    param1=1&param2=2&param3=3

Also if an object was excepted on wcf side, for instance following service contract
getData(Person p)

Where p is

class Person
{
    [DataMember]
    public string name;
    [DataMember]
    public string age;
}

Request Header will be the same as above but Request Body will be like:
Person.name=John Smith&Person.age=25

As a best practice, keep your Request Body case sensitive to avoid any issues.

No comments: