Movatterモバイル変換


[0]ホーム

URL:


GitHub SearchDjango REST framework
request.py

Requests

If you're doing REST-based web service stuff ... you should ignore request.POST.

— Malcom Tredinnick,Django developers group

REST framework'sRequest class extends the standardHttpRequest, adding support for REST framework's flexible request parsing and request authentication.


Request parsing

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.

.data

request.data returns the parsed content of the request body. This is similar to the standardrequest.POST andrequest.FILES attributes except that:

  • It includes all parsed content, includingfile and non-file inputs.
  • It supports parsing the content of HTTP methods other thanPOST, meaning that you can access the content ofPUT andPATCH requests.
  • It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incomingJSON data similarly to how you handle incomingform data.

For more details see theparsers documentation.

.query_params

request.query_params is a more correctly named synonym forrequest.GET.

For clarity inside your code, we recommend usingrequest.query_params instead of the Django's standardrequest.GET. Doing so will help keep your codebase more correct and obvious - any HTTP method type may include query parameters, not justGET requests.

.parsers

TheAPIView class or@api_view decorator will ensure that this property is automatically set to a list ofParser instances, based on theparser_classes set on the view or based on theDEFAULT_PARSER_CLASSES setting.

You won't typically need to access this property.

Note

If a client sends malformed content, then accessingrequest.data may raise aParseError. By default REST framework'sAPIView class or@api_view decorator will catch the error and return a400 Bad Request response.

If a client sends a request with a content-type that cannot be parsed then aUnsupportedMediaType exception will be raised, which by default will be caught and return a415 Unsupported Media Type response.

Content negotiation

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.

.accepted_renderer

The renderer instance that was selected by the content negotiation stage.

.accepted_media_type

A string representing the media type that was accepted by the content negotiation stage.


Authentication

REST framework provides flexible, per-request authentication, that gives you the ability to:

  • 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.

.user

request.user typically returns an instance ofdjango.contrib.auth.models.User, although the behavior depends on the authentication policy being used.

If the request is unauthenticated the default value ofrequest.user is an instance ofdjango.contrib.auth.models.AnonymousUser.

For more details see theauthentication documentation.

.auth

request.auth returns any additional authentication context. The exact behavior ofrequest.auth depends on the authentication policy being used, but it may typically be an instance of the token that the request was authenticated against.

If the request is unauthenticated, or if no additional context is present, the default value ofrequest.auth isNone.

For more details see theauthentication documentation.

.authenticators

TheAPIView class or@api_view decorator will ensure that this property is automatically set to a list ofAuthentication instances, based on theauthentication_classes set on the view or based on theDEFAULT_AUTHENTICATORS setting.

You won't typically need to access this property.

Note

You may see aWrappedAttributeError raised when calling the.user or.auth properties. These errors originate from an authenticator as a standardAttributeError, 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 theAttributeError 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.

Browser enhancements

REST framework supports a few browser enhancements such as browser-basedPUT,PATCH andDELETE forms.

.method

request.method returns theuppercased string representation of the request's HTTP method.

Browser-basedPUT,PATCH andDELETE forms are transparently supported.

For more information see thebrowser enhancements documentation.

.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 usingrequest.META.get('HTTP_CONTENT_TYPE'), as it provides transparent support for browser-based non-form content.

For more information see thebrowser enhancements documentation.

.stream

request.stream returns a stream representing the content of the request body.

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.


Standard HttpRequest attributes

As REST framework'sRequest extends Django'sHttpRequest, all the other standard attributes and methods are also available. For example therequest.META andrequest.session dictionaries are available as normal.

Note that due to implementation reasons theRequest class does not inherit fromHttpRequest class, but instead extends the class using composition.


[8]ページ先頭

©2009-2025 Movatter.jp