mshafrir
mshafrir

Reputation: 5230

How to execute the "less popular" HTTP actions in a RESTful web app?

I'm developing a Python web app as a learning exercise, and I am looking into making my app RESTful.

To that end, I want to be able to handle various types of HTTP actions/verbs where they are applicable. For example, if widget with id 12 is represented with the URI http://domain/widget/12, and I want to give the end user the ability to delete this widget, they should be able to make an HTTP DELETE request against /widget/12.

However, HTML forms only support GET and POST as far as I know, so how would I go about making an HTTP request with the "less popular" HTTP actions, such as DELETE?

Let's say that on the widget 12's view page (returned by HTTP GET), I want to include a form with just a single submit button to delete that widget. For example:

<form action="/widget/12" method="DELETE">
<input type="submit" value="Delete Me!" />
</form>

However, it's already established that HTML forms do not support DELETE for the method attribute. So what is the RESTful way of executing a DELETE request from the client in this situation?

Upvotes: 4

Views: 2196

Answers (3)

JMS
JMS

Reputation: 2193

Stephen Walther had a great blog post on this very topic today.

http://stephenwalther.com/blog/archive/2009/01/21/asp.net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx

Depending on how strictly you wish to adhere to the concept of being "RESTful", the use of POST to perform a delete won't appeal to you.

Upvotes: 1

Breton
Breton

Reputation: 15582

You either tunnel the commands through POST, or use Ajax, or both- (post tunneling acting as a fall back when javascript support isn't found)

Upvotes: 1

Tracker1
Tracker1

Reputation: 19344

From a browser, you will need to use XmlHttpRequest (Ajax) for the scenario you are describing. If your client, or server doesn't support the additional methods, it's become common to use the custom X-HTTP-Method-Override header to specify the action.

Upvotes: 7

Related Questions