aboutsummaryrefslogtreecommitdiffstats
path: root/docs/formoverloading.md
blob: cab47db95520ae46c36527e5e6d280491e1b79d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Supporting browser-based PUT & DELETE
=====================================

> "There are two noncontroversial uses for overloaded POST.  The first is to *simulate* HTTP's uniform interface for clients like web browsers that don't support PUT or DELETE" - [RESTful Web Services](1), Leonard Richardson & Sam Ruby.

This is the same strategy as is used in [Ruby on Rails](2).

Overloading the HTTP method
---------------------------

For example, given the following form:

    <form action="/news-items/5" method="POST">
	    <input type="hidden" name="_method" value="DELETE">
	</form>

`request.method` would return `"DELETE"`.

Overloading the HTTP content type
---------------------------------

Browser-based submission of content types other than form are supported by using form fields named `_content` and `_content_type`:

For example, given the following form:

    <form action="/news-items/5" method="PUT">
	    <input type="hidden" name="_content_type" value="application/json">
		<input name="_content" value="{'count': 1}">
	</form>

`request.content_type` would return `"application/json"`, and `request.content` would return `"{'count': 1}"`

Why not just use Javascript? 
============================

**[TODO]**

Doesn't HTML5 support PUT and DELETE forms?
===========================================

Nope.  It was at one point intended to support `PUT` and `DELETE` forms, but was later [dropped from the spec](3).  There remains [ongoing discussion](4) about adding support for `PUT` and `DELETE`, as well as how to support content-types other than form-encoded data.

[1]: http://www.amazon.com/Restful-Web-Services-Leonard-Richardson/dp/0596529260
[2]: http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-put-or-delete-methods-work
[3]: http://www.w3.org/TR/html5-diff/#changes-2010-06-24
[4]: http://amundsen.com/examples/put-delete-forms/