django-rest-framework/docs/api-guide/requests.md

143 lines
6.4 KiB
Markdown
Raw Normal View History

2012-09-09 01:06:13 +04:00
<a class="github" href="request.py"></a>
2012-09-01 23:26:27 +04:00
# Requests
2012-08-29 23:57:37 +04:00
> If you're doing REST-based web service stuff ... you should ignore request.POST.
>
2012-09-27 00:47:19 +04:00
> &mdash; Malcom Tredinnick, [Django developers group][cite]
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
REST framework's `Request` class extends the standard `HttpRequest`, adding support for REST framework's flexible request parsing and request authentication.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
---
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
# Request parsing
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
REST framework's Request objects provide flexible request parsing that allows you to treat requests with JSON data or other media types in the same way that you would normally deal with form data.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .DATA
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
`request.DATA` returns the parsed content of the request body. This is similar to the standard `request.POST` attribute except that:
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
* It supports parsing the content of HTTP methods other than `POST`, meaning that you can access the content of `PUT` and `PATCH` requests.
* It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incoming JSON data in the same way that you handle incoming form data.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For more details see the [parsers documentation].
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .FILES
2012-08-29 23:57:37 +04:00
2012-10-21 18:34:07 +04:00
`request.FILES` returns any uploaded files that may be present in the content of the request body. This is the same as the standard `HttpRequest` behavior, except that the same flexible request parsing is used for `request.DATA`.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For more details see the [parsers documentation].
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .QUERY_PARAMS
2012-08-29 23:57:37 +04:00
2012-10-21 18:34:07 +04:00
`request.QUERY_PARAMS` is a more correctly named synonym for `request.GET`.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For clarity inside your code, we recommend using `request.QUERY_PARAMS` instead of the usual `request.GET`, as *any* HTTP method type may include query parameters.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .parsers
2012-08-29 23:57:37 +04:00
2012-10-21 18:34:07 +04:00
The `APIView` class or `@api_view` decorator will ensure that this property is automatically set to a list of `Parser` instances, based on the `parser_classes` set on the view or based on the `DEFAULT_PARSER_CLASSES` setting.
2012-10-13 18:07:43 +04:00
You won't typically need to access this property.
---
**Note:** If a client sends malformed content, then accessing `request.DATA` or `request.FILES` may raise a `ParseError`. By default REST framework's `APIView` class or `@api_view` decorator will catch the error and return a `400 Bad Request` response.
2012-10-15 01:43:07 +04:00
If a client sends a request with a content-type that cannot be parsed then a `UnsupportedMediaType` exception will be raised, which by default will be caught and return a `415 Unsupported Media Type` response.
2012-10-13 18:07:43 +04:00
---
# Content negotiation
The request exposes some properties that allow you to determine the result of the content negotiation stage. This allows you to implement behaviour such as selecting a different serialisation schemes for different media types.
## .accepted_renderer
The renderer instance what was selected by the content negotiation stage.
## .accepted_media_type
A string representing the media type that was accepted by the content negotiation stage.
---
2012-10-13 18:07:43 +04:00
# Authentication
2012-10-21 18:34:07 +04:00
REST framework provides flexible, per-request authentication, that gives you the ability to:
2012-10-13 18:07:43 +04:00
* Use different authentication policies for different parts of your API.
* Support the use of multiple authentication policies.
* Provide both user and token information associated with the incoming request.
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
## .user
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
`request.user` typically returns an instance of `django.contrib.auth.models.User`, although the behavior depends on the authentication policy being used.
If the request is unauthenticated the default value of `request.user` is an instance of `django.contrib.auth.models.AnonymousUser`.
For more details see the [authentication documentation].
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
## .auth
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
`request.auth` returns any additional authentication context. The exact behavior of `request.auth` depends on the authentication policy being used, but it may typically be an instance of the token that the request was authenticated against.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
If the request is unauthenticated, or if no additional context is present, the default value of `request.auth` is `None`.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For more details see the [authentication documentation].
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .authenticators
2012-08-29 23:57:37 +04:00
2012-10-21 18:34:07 +04:00
The `APIView` class or `@api_view` decorator will ensure that this property is automatically set to a list of `Authentication` instances, based on the `authentication_classes` set on the view or based on the `DEFAULT_AUTHENTICATORS` setting.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
You won't typically need to access this property.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
---
2012-10-13 18:12:44 +04:00
# Browser enhancements
2012-10-13 18:07:43 +04:00
2013-01-17 15:17:53 +04:00
REST framework supports a few browser enhancements such as browser-based `PUT`, `PATCH` and `DELETE` forms.
2012-10-13 18:07:43 +04:00
## .method
`request.method` returns the **uppercased** string representation of the request's HTTP method.
2013-01-17 15:17:53 +04:00
Browser-based `PUT`, `PATCH` and `DELETE` forms are transparently supported.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For more information see the [browser enhancements documentation].
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
## .content_type
`request.content_type`, returns a string object representing the media type of the HTTP request's body, or an empty string if no media type was provided.
You won't typically need to directly access the request's content type, as you'll normally rely on REST framework's default request parsing behavior.
If you do need to access the content type of the request you should use the `.content_type` property in preference to using `request.META.get('HTTP_CONTENT_TYPE')`, as it provides transparent support for browser-based non-form content.
For more information see the [browser enhancements documentation].
## .stream
`request.stream` returns a stream representing the content of the request body.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
You won't typically need to directly access the request's content, as you'll normally rely on REST framework's default request parsing behavior.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
If you do need to access the raw content directly, you should use the `.stream` property in preference to using `request.content`, as it provides transparent support for browser-based non-form content.
2012-08-29 23:57:37 +04:00
2012-10-13 18:07:43 +04:00
For more information see the [browser enhancements documentation].
2012-08-29 23:57:37 +04:00
2012-10-14 23:46:38 +04:00
---
# Standard HttpRequest attributes
As REST framework's `Request` extends Django's `HttpRequest`, all the other standard attributes and methods are also available. For example the `request.META` and `request.session` dictionaries are available as normal.
2012-10-14 23:46:38 +04:00
Note that due to implementation reasons the `Request` class does not inherit from `HttpRequest` class, but instead extends the class using composition.
[cite]: https://groups.google.com/d/topic/django-developers/dxI4qVzrBY4/discussion
2012-10-13 18:07:43 +04:00
[parsers documentation]: parsers.md
[authentication documentation]: authentication.md
[browser enhancements documentation]: ../topics/browser-enhancements.md