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

28 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
  15. However we do agree that there are a lot of differences in the way some of these web browsers operate and the various scripting and web coding languages they use and their interpretation of this.Kttp

    ReplyDelete
  16. In the event that you don't yet have a website, the initial step is to record precisely why you need a website and what you need it to improve the situation your business. hybrid app development

    ReplyDelete
  17. I suggest that you look around and see what the various web hosting companies have to offer before you make that final choice. www.ldainteractive.com

    ReplyDelete

  18. This is genuinely an awesome read for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
    Web Development

    ReplyDelete
  19. Hey, great post! I also interested in Fiddler and I think you might like this article - vue vs react

    ReplyDelete
  20. This is a front-end JavaScript library developed by Facebook. Most of the major applications are currently running on React.JS. The applications which required data changes constantly are using React.JS.

    ReplyDelete
  21. I found this as an informative and interesting post, so i think it is very useful and knowledgeable.- gta 5 cheats

    ReplyDelete

  22. Respecting the time and exertion you put into your site and point by point data you offer!..
    Web Development

    ReplyDelete
  23. This current Google's child should be in the best position of web structures.

    ReplyDelete
  24. Getting reliable SEO services has become a very tedious task now. Thank God that Canada's SEO Company is here to resolve our issues. Canada's SEO Company

    ReplyDelete