find a location for property in a new city

Tuesday, 4 January 2011

How to POST to REST WCF web services with Fiddler

After making a set of REST web services with WCF I tested them by making requests with a browser. This is fine for GET requests however you will need some HTTP building tool such as Fiddler to test a POST, PUT or DELETE REST web service.

To begin with I have a WCF REST web service that intends for a Comment object to be POSTed:
[WebInvoke(UriTemplate = "comment", Method = "POST",
  RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
public Comment Comment(Comment comment)
{
    return comment;
}

The definition of Comment is simple:
[DataContract(Namespace = "")]
public class Comment
{
    [DataMember]
    public string Body { get; set; }
}

Note this namespace is very important as it will need to be specified in the XML sent in the request. Personally I set my DataContract with no namespace to save having to write it out in the XML every time.

Now open up Fiddler and lets get testing:

Create a POST to your REST service in Fiddler

  1. Go to the Request Builder tab in Fiddler.
  2. Set the verb to POST
  3. Set the URL to the one of your service
  4. Type "Content-Type: application/xml" into the Request Headers. (This is the bit that got be stumped for hours)
  5. Add your XML to the Request Body like <Comment><Body>colin</Body></Comment>

If you did give your DataContract a namespace other than blank as I did you will need to specify it in the XML of your Request Body. Such as: <Comment xmlns="MyNameSpace"><Body>colin</Body></Comment>

Happy testing!

Follow britishdev on Twitter

16 comments:

  1. Thanks I did it . It works great

    ReplyDelete
  2. www.oniontest.org (EASY/SIMPLE) rest/http web service testing without mocking yourself to an imaginary world ! - Write an Onion !

    ReplyDelete
  3. That's a great way to test our services, really. I was struggling with all the work it takes to mock up my services until I found your post. Thank you so much!

    ReplyDelete
  4. Even If you have blank namespace you have to put
    xmlns="" in the request body ...

    http://pantestmb.blogspot.com/2011/10/pass-multiple-body-parameters-wcf-rest.html

    Whithout xmlns="" in the request body , problems can appear

    ReplyDelete
  5. Thanks for the tip but I'll be honest, I have never found any problems myself nor have the iPhone developers that consumed this API.

    What problems were you experiencing exactly? Just for my benefit in case I have missed something.

    ReplyDelete
  6. Hello, Thanks for the great tutorial. I tried to extend the class with inserting DateTime? variable in it as following:-

    [DataContract(Namespace = "")]
    public class Comment
    {
    [DataMember]
    public string BodyComment { get; set; }
    [DataMember]
    public DateTime? CreatedDate { get; set; }
    }

    And followed everything else as you mentioned in the tutorial.
    But when I'm sending a POST request like this:
    xyz, or
    xyz
    As I want CreatedDate to be empty, it is giving me "Http 400 Bad request error".

    Kindly suggest if I'm doing something wrong.

    ReplyDelete
    Replies
    1. Sorry POST didn't come, but it is like
      I'm sending xyz in body and empty CreatedDate...

      Delete
    2. Bit hard to debug this kind of thing remotely. Where is the exception being thrown and what is it when you are debugging?

      Delete
  7. Thank you so much. I have been trying to understand the WebInvoke attribute for a few days and your example made it clear.

    ReplyDelete
  8. Thank you so much, this is exactly what I was looking for.

    ReplyDelete
  9. Fantastic post, been knocking myself out working out why object was not posting correctly, the namespace did it.

    ReplyDelete
  10. Why is my Fiddler meets "415 Unsupported Media Type"?
    I do everything in your example...

    ReplyDelete
  11. This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.Contact Us Content

    ReplyDelete
  12. Thanks for sharing a very helpful AND ALSO very informative blogPromotion King

    ReplyDelete
  13. Online medium and audio visual media is undoubtedly the strongest medium these days with extensive reach to the audience.
    create your brand

    ReplyDelete
  14. With this, you ought to just trust your well deserved cash and speculation to an Ecommerce Web Development Company that brags a past filled with mastery and polished skill in the business.easy proven way to start a blog

    ReplyDelete