If you're doing REST-based web service stuff ... you should ignore request.POST
— Malcom Tredinnick, Django developers group
REST framework's Request 类扩展了标准的 HttpRequest 类,同时加入了 REST 框架中灵活的 请求解析parsing 和 请求认证authentication 技术。
1. Request Parsing
Request 类提供了灵活的请求解析技术,因此你可以向处理表单数据那样处理带有 JSON 数据或者其他 媒体类型的 request 请求。
1.1 .data
request.data 返回经过解析后的 请求体数据,这和 标准的request.POST 和 request.FILES 类似,其不同点在于:
1. 它包含所有解析过的数据,包括文件或者非文件的输入;
2. 提供解析其他方法携带的数据,如 PUT,PATCH,不局限于 POST;
3. 支持灵活的 请求解析,并不局限于表单数据。如你可以像处理保单数据那样处理上传的 JSON 数据;
更多 细节见 parsing document
1.2 .query_params
request.query_params 可以说是 request.GET 的代名词。推荐使用 它代替 标准的 django request.GET,以保持代码的清晰,毕竟所有的 HTTP 方法都能拥有 查询字符串;
1.3 .parsers
APIView 类 或者 @api_view 装饰器 会保证装饰的属性会自动绑定到一系列解析实例,根据 在 view 上设置的parser_classes 或者 默认设置中的 DEFAULT_PARSER_CLASSES 设置。
You won't typically need to access this property.
Note: If a client sends malformed(异常的) content, then accessing
request.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 a
UnsupportedMediaType
exception will be raised, which by default will be caught and return a415 Unsupported Media Type
response.
2. 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.
3. Authentication 认证
REST framework provides flexible, per-request authentication, that gives you the ability to:
1 对API 中不同的部分使用不同认证策略
2 支持多认证策略
3 同时提供进来的请求的 user 和 token 信息
3.1 .user
request.user
返回一个 django.contrib.auth.models.User 的实例,虽然后续的行为依赖使用的认证策略。
如果没有认证, request.user 是一个 django.contrib.auth.models.AnonymousUser
For more details see the authentication documentation.
3.2 .auth
request.auth 返回任何 附加的 认证 上下文,
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.
If the request is unauthenticated, or if no additional context is present, the default value of request.auth
is None
.
For more details see the authentication documentation.
3.3 .authenticators
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.
You won't typically need to access this property.
4. Browser enhancements 浏览器增强
REST framework supports a few browser enhancements such as browser- based PUT
, PATCH
and DELETE
forms.
4.1 .method
request.method
returns the uppercased string representation of the request's HTTP method.
Browser-based PUT
, PATCH
and DELETE
forms are transparently supported.
For more information see the browser enhancements documentation.
4.2 .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.
4.3 .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.
If you do need to access the raw content directly, you should use the .stream
property in preference to usingrequest.content
, as it provides transparent support for browser-based non-form content.
For more information see the browser enhancements documentation.
Standard HttpRequest attributes
As REST framework's
Request
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 the
Request
class does not inherit fromHttpRequest
class, but instead extends the class using composition.(使用 组合 而不是继承)