request.py

Requests

如果你正在做基于REST的Web服务......你应该忽略request.POST。

— Malcom Tredinnick, Django developers group

REST框架的Request类扩展了标准HttpRequest,增加了对REST框架灵活请求解析和请求身份验证的支持。


Request parsing

REST框架的Request对象提供灵活的请求解析,允许您以与通常处理表单数据相同的方式处理具有JSON数据或其他媒体类型的请求。

.data

request.data返回请求正文的已解析内容。 这类似于标准的request.POSTrequest.FILES属性,除了:

  • 它包括所有已解析的内容,包括file and non-file输入。
  • 它支持解析POST以外的HTTP方法的内容,这意味着您可以访问PUTPATCH请求的内容。
  • 它支持REST框架的灵活请求解析,而不仅仅支持表单数据。 例如,您可以像处理传入表单数据一样处理传入的JSON数据。

有关更多详细信息,请参阅解析器文档

.query_params

request.query_paramsrequest.GET的更正确命名的同义词。

为清楚起见,我们建议使用request.query_params而不是Django的标准request.GET 这样做有助于保持代码库更加正确和明显 - 任何HTTP方法类型都可能包含查询参数,而不仅仅是GET请求。

.parsers

APIView类或@api_view装饰器将确保基于parser_classes将此属性自动设置为Parser实例列表在视图上设置或基于DEFAULT_PARSER_CLASSES设置。

您通常不需要访问此属性。


Note: 如果客户端发送格式错误的内容,则访问request.data可能会引发ParseError 默认情况下,REST框架的APIView类或@api_view装饰器将捕获错误并返回400 Bad Request响应。

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.


Content negotiation

该请求公开了一些允许您确定内容协商阶段结果的属性。 这允许您实现诸如为不同媒体类型选择不同序列化方案之类的行为。

.accepted_renderer

渲染器实例由内容协商阶段选择的内容。

.accepted_media_type

表示内容协商阶段接受的媒体类型的字符串。


Authentication

REST框架提供灵活的按请求身份验证,使您能够:

  • 每个API都可拥有自定义的身份验证策略
  • 支持使用多种身份验证策略。
  • 提供与传入请求关联的用户和令牌信息。

.user

request.user通常会返回django.contrib.auth.models.User的实例,但行为取决于所使用的身份验证策略。

如果请求未经身份验证,则request.user的默认值是django.contrib.auth.models.AnonymousUser的实例。

更多详情 〉 authentication documentation.

.auth

request.auth返回任何其他身份验证上下文。 request.auth的确切行为取决于所使用的身份验证策略,但它通常可能是请求经过身份验证的令牌实例。

如果请求未经身份验证,或者没有其他上下文,则request.auth的默认值为None

更多详情 〉 authentication documentation.

.authenticators

APIView类或@api_view装饰器将根据authentication_classes确保此属性自动设置为Authentication实例列表在视图上设置或基于DEFAULT_AUTHENTICATORS设置。

You won't typically need to access this property.


Note: 调用.user.auth属性时,您可能会看到WrappedAttributeError 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 orginates from the authenticator and will instaed assume that the request object does not have a .user or .auth property. The authenticator will need to be fixed.


Browser enhancements

REST框架支持一些浏览器增强功能,例如基于浏览器的PUTPATCHDELETE表单。

.method

request.method返回请求的HTTP方法的uppercased字符串表示形式。

基于浏览器的PUTPATCHDELETE表单是透明支持的。

For more information see the browser enhancements documentation.

.content_type

request.content_type,返回表示HTTP请求正文的媒体类型的字符串对象,如果没有提供媒体类型则返回空字符串。

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返回表示请求正文内容的流。

您通常不需要直接访问请求的内容,因为您通常会依赖REST框架的默认请求解析行为。


Standard HttpRequest attributes

由于REST框架的Request扩展了Django的HttpRequest,所以其他所有标准属性和方法也都可用。 例如,request.METArequest.session字典正常可用。

请注意,由于实现原因,Request类不从HttpRequest类继承,而是使用composition扩展类。