美文网首页
Rest-framework-API参考-Requests

Rest-framework-API参考-Requests

作者: 阳光小镇少爷 | 来源:发表于2017-04-19 17:06 被阅读0次

    Requests请求

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

    — Malcom Tredinnick,Django developers group

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

    REST架构的请求类扩展了标准的HttpRequest,加入支持REST架构的灵活解析请求和请求认证。

    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.

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

    .data

    request.datareturns the parsed content of the request body. This is similar to the standardrequest.POSTandrequest.FILESattributes except that:

    request.data返回解析内容请求体。这是类似于标准的request.post和request.files属性。除了:

    It includes all parsed content, includingfile and non-fileinputs.

    包括所有分析的内容,包括文件和非文件输入。

    It supports parsing the content of HTTP methods other thanPOST, meaning that you can access the content ofPUTandPATCHrequests.

    支持解析HTTP方法以外的内容后,意味着你可以访问请求的内容和补丁。

    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.

    支持REST架构的灵活的解析请求,而不是仅仅支持表单数据。例如,您可以像传入表单数据一样处理传入的JSON数据。

    For more details see theparsers documentation.

    .query_params

    request.query_paramsis a more correctly named synonym forrequest.GET.

    For clarity inside your code, we recommend usingrequest.query_paramsinstead 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 justGETrequests.

    request.query_params是更正确地命名为request.get同义词。

    在你的代码清晰,我们建议使用request.query_params代替Django的标准request.get。这样做将有助于让你的代码更正确、明显的任何HTTP方法类型包括查询参数,不只是GET请求。

    .parsers

    TheAPIViewclass or@api_viewdecorator will ensure that this property is automatically set to a list ofParserinstances, based on theparser_classesset on the view or based on theDEFAULT_PARSER_CLASSESsetting.

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

    You won't typically need to access this property.

    Note:If a client sends malformed content, then accessingrequest.datamay raise aParseError. By default REST framework'sAPIViewclass or@api_viewdecorator will catch the error and return a400 Bad Requestresponse.

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

    注意:如果一个客户端发送格式不正确的内容,然后访问request.data可能引起错误分析。默认情况下,REST的apiview框架类或@api_view装饰将捕获错误,并返回一个400错误的请求的响应。

    如果一个客户端发送一个内容类型,不能被解析并unsupportedmediatype将引发异常的请求,默认情况下会被抓到并返回一个415响应不支持的媒体类型。

    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.

    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.

    认证

    REST框架提供灵活的,每个请求认证,这给你的能力:

    ●使用不同的认证政策,你的API的不同部分。

    ●支持多种认证政策的使用。

    ●提供用户令牌信息与请求相关的。

    .user

    request.usertypically 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.useris an instance ofdjango.contrib.auth.models.AnonymousUser.

    For more details see theauthentication documentation.

    request.user通常返回django.contrib.auth.models.User实例,虽然行为取决于所使用的认证政策。

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

    有关详细信息请参见验证文档。

    .auth

    request.authreturns any additional authentication context. The exact behavior ofrequest.authdepends 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.authisNone.

    For more details see theauthentication documentation.

    request.auth返回的任何额外的认证上下文。对request.auth准确的行为取决于认证策略被使用,但它可能是典型的令牌请求认证针对实例。

    如果请求是未经验证的,如果没有额外的上下文是存在的,request.auth默认值是没有的。

    .authenticators

    TheAPIViewclass or@api_viewdecorator will ensure that this property is automatically set to a list ofAuthenticationinstances, based on theauthentication_classesset on the view or based on theDEFAULT_AUTHENTICATORSsetting.

    You won't typically need to access this property.

    APIView类或@api_view装饰将确保此属性自动设置为列表中的Authenticationinstances,基于authentication_classes视图上设置或基于default_authenticators设置。

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

    Browser enhancements

    REST framework supports a few browser enhancements such as browser-basedPUT,PATCHandDELETEforms.

    REST框架支持一些浏览器增强功能,如基于浏览器的PUT,PATCH和DELETE表单。

    .method

    request.methodreturns theuppercasedstring representation of the request's HTTP method.

    Browser-basedPUT,PATCHandDELETEforms are transparently supported.

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

    基于浏览器的PUT,PATCHandDELETE表单是透明支持的。

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

    request.content_type,字符串对象代表了回归的媒体类型HTTP的身体,一个空的字符串,如果没有提供媒体类型。

    .stream

    request.streamreturns 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'sRequestextends Django'sHttpRequest, all the other standard attributes and methods are also available. For example therequest.METAandrequest.sessiondictionaries are available as normal.

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

    相关文章

      网友评论

          本文标题:Rest-framework-API参考-Requests

          本文链接:https://www.haomeiwen.com/subject/gflhzttx.html