Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← Tools: Add In Express
    • Visual Studio 2008 and its CopyLocal setting →

    REST: How to respond to an HTTP POST

    24 Mar 2009 by Dean in HTML, Programming, REST, Web Services / 1 Comment

    I ran into a problem yesterday. I’d sent a HTTP POST request to a collection of resources on our RESTful web service. Our server responded with an HTTP 201: Created status code and the URI of the new resource in the Location header.

    And then… nothing happened.

    This was not what I was expecting. I expected my web browser to follow up with a GET request to the URI I’d provided. But Firefox 3 wasn’t biting. A problem with Firefox? I checked in IE8 and Google Chrome and got the same behavior.

    Had I misread the HTTP spec? Did I misunderstand the REST pattern? I grabbed for my worn copy of RESTful Web Services. Nope. HTTP 201 seemed to be the right status for this situation.

    Had I blundered into some common error? I checked Stefan Tilkov’s useful list of REST Antipatterns. But I couldn’t find anything that quite matched my situation.

    I started Googling, but couldn’t find much apart from this question about HTTP Post on Stack Overflow. There were some cryptic responses (to which I’ve added my own answer now).

    Eventually, I discovered what I needed to know from Ben Ramsey’s article on HTTP redirection. It’s part of his series discussing RFC 2616, which describes the HTTP/1.1 protocol.

    The answer is that while web service clients will often “take the hint” provided by a HTTP 201: Created response, web browsers won’t. If you actually want a web browser to go somewhere else, you need to send a status code in the 3xx series. In this situation, the status code you want is HTTP 303: See Other.

    Once I changed the status code returned by the server, all the web browsers followed up the response with a GET to the new URI.

    Related posts

    1. Using WCF for REST, Part 2
    2. Using WCF for REST, Part 3
    3. Using WCF for REST, Part 1
    4. WCF Bad Request
    5. Review: Restful Web Services
    • Tweet
    • Tags:
    • http
    • REST
    • RESTful

    1 Comment

    • Peter Obiefuna

      Forcing all create/update events to return a redirect is a bad idea. It should still return a 201 IMO. Let the consuming client decide what to do with the returned resource uri.

      In most cases, it’s a wasted round trip. In some cases, for instance most persons just simply return “Record created successfully” and provide a link to view/update/delete the record based on user privileges. In others, user may not even be aware that the record was created. Moreover, the create event may be a part of a composited whole.

      So, let the create process be concerned with what it does. Create, and return a 201 and end it’s concern.

      12 Apr 2010 12:04 pm
      Reply

      Leave a Comment

      Posting your comment...

      Subscribe to these comments via email

      • Categories

        • .NET (41)
        • AJAX (3)
        • Books (7)
        • HTML (9)
        • Infovark (8)
        • Programming (48)
        • REST (11)
        • SQL (3)
        • Testing (3)
        • Tools (13)
        • UI (3)
        • WCF (11)
        • Web Services (8)
        • WPF (4)
        • XML (4)
      • Archives

      • Get future articles


         

      • Blogroll

        • Ajaxian
        • Anne Van Kesteren
        • Brain.Save()
        • Coding Horror
        • Eric Sink
        • Joel Spolsky
        • John Resig
        • Mark Pilgrim
        • Raymond Chen
        • Scott Hansleman
        • Secret Geek
        • Steve Yegge
        • The Daily WTF
        • The Database Programmer
      • Meta

        • Log in
        • Entries RSS
        • Comments RSS
        • WordPress.org
    • Site map

      • News
      • Product
      • Download
      • Buy
      • Support
      • About
    • Recent Posts

      • Review: Brownfield Application Development in .NET
      • Using Modal Dialogs with a Splash Screen in WPF
      • Highlighting query terms in a WPF TextBlock
      • Getting XAML Hyperlink text to wrap
      • How to format the XAML Hyperlink NavigateUri
    • Twitter

      Copyright 2011 Infovark, Inc. All rights reserved.