| 1 |
# Browser enhancements |
| 2 |
|
| 3 |
> "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" |
| 4 |
> |
| 5 |
> — [RESTful Web Services][cite], Leonard Richardson & Sam Ruby. |
| 6 |
|
| 7 |
In order to allow the browsable API to function, there are a couple of browser enhancements that REST framework needs to provide. |
| 8 |
|
| 9 |
As of version 3.3.0 onwards these are enabled with javascript, using the [ajax-form][ajax-form] library. |
| 10 |
|
| 11 |
## Browser based PUT, DELETE, etc... |
| 12 |
|
| 13 |
The [AJAX form library][ajax-form] supports browser-based `PUT`, `DELETE` and other methods on HTML forms. |
| 14 |
|
| 15 |
After including the library, use the `data-method` attribute on the form, like so: |
| 16 |
|
| 17 |
<form action="/" data-method="PUT"> |
| 18 |
<input name='foo'/> |
| 19 |
... |
| 20 |
</form> |
| 21 |
|
| 22 |
Note that prior to 3.3.0, this support was server-side rather than javascript based. The method overloading style (as used in [Ruby on Rails][rails]) is no longer supported due to subtle issues that it introduces in request parsing. |
| 23 |
|
| 24 |
## Browser based submission of non-form content |
| 25 |
|
| 26 |
Browser-based submission of content types such as JSON are supported by the [AJAX form library][ajax-form], using form fields with `data-override='content-type'` and `data-override='content'` attributes. |
| 27 |
|
| 28 |
For example: |
| 29 |
|
| 30 |
<form action="/"> |
| 31 |
<input data-override='content-type' value='application/json' type='hidden'/> |
| 32 |
<textarea data-override='content'>{}</textarea> |
| 33 |
<input type="submit"/> |
| 34 |
</form> |
| 35 |
|
| 36 |
Note that prior to 3.3.0, this support was server-side rather than javascript based. |
| 37 |
|
| 38 |
## URL based format suffixes |
| 39 |
|
| 40 |
REST framework can take `?format=json` style URL parameters, which can be a |
| 41 |
useful shortcut for determining which content type should be returned from |
| 42 |
the view. |
| 43 |
|
| 44 |
This behavior is controlled using the `URL_FORMAT_OVERRIDE` setting. |
| 45 |
|
| 46 |
## HTTP header based method overriding |
| 47 |
|
| 48 |
Prior to version 3.3.0 the semi extension header `X-HTTP-Method-Override` was supported for overriding the request method. This behavior is no longer in core, but can be adding if needed using middleware. |
| 49 |
|
| 50 |
For example: |
| 51 |
|
| 52 |
METHOD_OVERRIDE_HEADER = 'HTTP_X_HTTP_METHOD_OVERRIDE' |
| 53 |
|
| 54 |
class MethodOverrideMiddleware: |
| 55 |
|
| 56 |
def __init__(self, get_response): |
| 57 |
self.get_response = get_response |
| 58 |
|
| 59 |
def __call__(self, request): |
| 60 |
if request.method == 'POST' and METHOD_OVERRIDE_HEADER in request.META: |
| 61 |
request.method = request.META[METHOD_OVERRIDE_HEADER] |
| 62 |
return self.get_response(request) |
| 63 |
|
| 64 |
## URL based accept headers |
| 65 |
|
| 66 |
Until version 3.3.0 REST framework included built-in support for `?accept=application/json` style URL parameters, which would allow the `Accept` header to be overridden. |
| 67 |
|
| 68 |
Since the introduction of the content negotiation API this behavior is no longer included in core, but may be added using a custom content negotiation class, if needed. |
| 69 |
|
| 70 |
For example: |
| 71 |
|
| 72 |
class AcceptQueryParamOverride() |
| 73 |
def get_accept_list(self, request): |
| 74 |
header = request.META.get('HTTP_ACCEPT', '*/*') |
| 75 |
header = request.query_params.get('_accept', header) |
| 76 |
return [token.strip() for token in header.split(',')] |
| 77 |
|
| 78 |
## Doesn't HTML5 support PUT and DELETE forms? |
| 79 |
|
| 80 |
Nope. It was at one point intended to support `PUT` and `DELETE` forms, but |
| 81 |
was later [dropped from the spec][html5]. There remains |
| 82 |
[ongoing discussion][put_delete] about adding support for `PUT` and `DELETE`, |
| 83 |
as well as how to support content types other than form-encoded data. |
| 84 |
|
| 85 |
[cite]: https://www.amazon.com/RESTful-Web-Services-Leonard-Richardson/dp/0596529260 |
| 86 |
[ajax-form]: https://github.com/tomchristie/ajax-form |
| 87 |
[rails]: https://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-put-or-delete-methods-work |
| 88 |
[html5]: https://www.w3.org/TR/html5-diff/#changes-2010-06-24 |
| 89 |
[put_delete]: http://amundsen.com/examples/put-delete-forms/ |