mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Update browser enhancments docs. Fixes #339.
This commit is contained in:
parent
e5777a6168
commit
5f0d4ef2fc
|
@ -2,42 +2,63 @@
|
||||||
|
|
||||||
> "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"
|
> "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.
|
> — [RESTful Web Services][cite], Leonard Richardson & Sam Ruby.
|
||||||
|
|
||||||
## Browser based PUT, DELETE, etc...
|
## Browser based PUT, DELETE, etc...
|
||||||
|
|
||||||
**TODO: Preamble.** Note that this is the same strategy as is used in [Ruby on Rails](2).
|
REST framework supports browser-based `PUT`, `DELETE` and other methods, by
|
||||||
|
overloading `POST` requests using a hidden form field.
|
||||||
|
|
||||||
|
Note that this is the same strategy as is used in [Ruby on Rails][rails].
|
||||||
|
|
||||||
For example, given the following form:
|
For example, given the following form:
|
||||||
|
|
||||||
<form action="/news-items/5" method="POST">
|
<form action="/news-items/5" method="POST">
|
||||||
<input type="hidden" name="_method" value="DELETE">
|
<input type="hidden" name="_method" value="DELETE">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
`request.method` would return `"DELETE"`.
|
`request.method` would return `"DELETE"`.
|
||||||
|
|
||||||
## Browser based submission of non-form content
|
## Browser based submission of non-form content
|
||||||
|
|
||||||
Browser-based submission of content types other than form are supported by using form fields named `_content` and `_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:
|
For example, given the following form:
|
||||||
|
|
||||||
<form action="/news-items/5" method="PUT">
|
<form action="/news-items/5" method="PUT">
|
||||||
<input type="hidden" name="_content_type" value="application/json">
|
<input type="hidden" name="_content_type" value="application/json">
|
||||||
<input name="_content" value="{'count': 1}">
|
<input name="_content" value="{'count': 1}">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
`request.content_type` would return `"application/json"`, and `request.stream` would return `"{'count': 1}"`
|
`request.content_type` would return `"application/json"`, and
|
||||||
|
`request.stream` would return `"{'count': 1}"`
|
||||||
|
|
||||||
## URL based accept headers
|
## URL based accept headers
|
||||||
|
|
||||||
|
REST framework can take `?accept=application/json` style URL parameters,
|
||||||
|
which allow the `Accept` header to be overridden.
|
||||||
|
|
||||||
|
This can be useful for testing the API from a web browser, where you don't
|
||||||
|
have any control over what is sent in the `Accept` header.
|
||||||
|
|
||||||
## URL based format suffixes
|
## URL based format suffixes
|
||||||
|
|
||||||
|
REST framework can take `?format=json` style URL parameters, which can be a
|
||||||
|
useful shortcut for determing which content type should be returned from
|
||||||
|
the view.
|
||||||
|
|
||||||
|
This is a more concise than using the `accept` override, but it also gives
|
||||||
|
you less control. (For example you can't specify any media type parameters)
|
||||||
|
|
||||||
## Doesn't HTML5 support PUT and DELETE forms?
|
## 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.
|
Nope. It was at one point intended to support `PUT` and `DELETE` forms, but
|
||||||
|
was later [dropped from the spec][html5]. There remains
|
||||||
|
[ongoing discussion][put_delete] 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
|
[cite]: 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
|
[rails]: 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
|
[html5]: http://www.w3.org/TR/html5-diff/#changes-2010-06-24
|
||||||
[4]: http://amundsen.com/examples/put-delete-forms/
|
[put_delete]: http://amundsen.com/examples/put-delete-forms/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user