Debian Codegraph @

docs/api-guide/requests.md

raw
1 ---
2 source:
3 - request.py
4 ---
5
6 > If you're doing REST-based web service stuff ... you should ignore request.POST.
7 >
8 > — Malcolm Tredinnick, [Django developers group][cite]
9
10 REST framework's `Request` class extends the standard `HttpRequest`, adding support for REST framework's flexible request parsing and request authentication.
11
12 ---
13
14 ## Request parsing
15
16 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.
17
18 ### .data
19
20 `request.data` returns the parsed content of the request body. This is similar to the standard `request.POST` and `request.FILES` attributes except that:
21
22 * It includes all parsed content, including *file and non-file* inputs.
23 * It supports parsing the content of HTTP methods other than `POST`, meaning that you can access the content of `PUT` and `PATCH` requests.
24 * It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incoming [JSON data] similarly to how you handle incoming [form data].
25
26 For more details see the [parsers documentation].
27
28 ### .query_params
29
30 `request.query_params` is a more correctly named synonym for `request.GET`.
31
32 For clarity inside your code, we recommend using `request.query_params` instead of the Django's standard `request.GET`. Doing so will help keep your codebase more correct and obvious - any HTTP method type may include query parameters, not just `GET` requests.
33
34 ### .parsers
35
36 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.
37
38 You won't typically need to access this property.
39
40 !!! note
41 If a client sends malformed content, then accessing `request.data` 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.
42
43 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.
44
45 ## Content negotiation
46
47 The request exposes some properties that allow you to determine the result of the content negotiation stage. This allows you to implement behavior such as selecting a different serialization schemes for different media types.
48
49 ### .accepted_renderer
50
51 The renderer instance that was selected by the content negotiation stage.
52
53 ### .accepted_media_type
54
55 A string representing the media type that was accepted by the content negotiation stage.
56
57 ---
58
59 ## Authentication
60
61 REST framework provides flexible, per-request authentication, that gives you the ability to:
62
63 * Use different authentication policies for different parts of your API.
64 * Support the use of multiple authentication policies.
65 * Provide both user and token information associated with the incoming request.
66
67 ### .user
68
69 `request.user` typically returns an instance of `django.contrib.auth.models.User`, although the behavior depends on the authentication policy being used.
70
71 If the request is unauthenticated the default value of `request.user` is an instance of `django.contrib.auth.models.AnonymousUser`.
72
73 For more details see the [authentication documentation].
74
75 ### .auth
76
77 `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.
78
79 If the request is unauthenticated, or if no additional context is present, the default value of `request.auth` is `None`.
80
81 For more details see the [authentication documentation].
82
83 ### .authenticators
84
85 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.
86
87 You won't typically need to access this property.
88
89 !!! note
90 You may see a `WrappedAttributeError` raised when calling the `.user` or `.auth` properties. These errors originate from an authenticator as a standard `AttributeError`, however it's necessary that they be re-raised as a different exception type in order to prevent them from being suppressed by the outer property access. Python will not recognize that the `AttributeError` originates from the authenticator and will instead assume that the request object does not have a `.user` or `.auth` property. The authenticator will need to be fixed.
91
92 ## Browser enhancements
93
94 REST framework supports a few browser enhancements such as browser-based `PUT`, `PATCH` and `DELETE` forms.
95
96 ### .method
97
98 `request.method` returns the **uppercased** string representation of the request's HTTP method.
99
100 Browser-based `PUT`, `PATCH` and `DELETE` forms are transparently supported.
101
102 For more information see the [browser enhancements documentation].
103
104 ### .content_type
105
106 `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.
107
108 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.
109
110 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.
111
112 For more information see the [browser enhancements documentation].
113
114 ### .stream
115
116 `request.stream` returns a stream representing the content of the request body.
117
118 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.
119
120 ---
121
122 ## Standard HttpRequest attributes
123
124 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.
125
126 Note that due to implementation reasons the `Request` class does not inherit from `HttpRequest` class, but instead extends the class using composition.
127
128
129 [cite]: https://groups.google.com/d/topic/django-developers/dxI4qVzrBY4/discussion
130 [parsers documentation]: parsers.md
131 [JSON data]: parsers.md#jsonparser
132 [form data]: parsers.md#formparser
133 [authentication documentation]: authentication.md
134 [browser enhancements documentation]: ../topics/browser-enhancements.md