From e628d9eb9b7deac2ecffe23eace5c72709887f8f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 6 Mar 2015 12:05:16 +0000 Subject: Update documentation --- api-guide/parsers/index.html | 102 ++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 44 deletions(-) (limited to 'api-guide/parsers') diff --git a/api-guide/parsers/index.html b/api-guide/parsers/index.html index e04f563e..132d146d 100644 --- a/api-guide/parsers/index.html +++ b/api-guide/parsers/index.html @@ -188,6 +188,10 @@ Pagination +
  • + Versioning +
  • +
  • Content negotiation
  • @@ -231,6 +235,10 @@ Documenting your API +
  • + Internationalization +
  • +
  • AJAX, CSRF & CORS
  • @@ -260,23 +268,11 @@
  • - 2.0 Announcement -
  • - -
  • - 2.2 Announcement -
  • - -
  • - 2.3 Announcement -
  • - -
  • - 2.4 Announcement + 3.0 Announcement
  • - 3.0 Announcement + 3.1 Announcement
  • @@ -287,10 +283,6 @@ Release Notes
  • -
  • - Credits -
  • - @@ -377,14 +369,6 @@ JSONParser -
  • - YAMLParser -
  • - -
  • - XMLParser -
  • -
  • FormParser
  • @@ -429,6 +413,14 @@ +
  • + YAML +
  • + +
  • + XML +
  • +
  • MessagePack
  • @@ -472,34 +464,34 @@ sending more complex data than simple forms

    As an example, if you are sending json encoded data using jQuery with the .ajax() method, you should make sure to include the contentType: 'application/json' setting.


    Setting the parsers

    -

    The default set of parsers may be set globally, using the DEFAULT_PARSER_CLASSES setting. For example, the following settings would allow requests with YAML content.

    +

    The default set of parsers may be set globally, using the DEFAULT_PARSER_CLASSES setting. For example, the following settings would allow only requests with JSON content, instead of the default of JSON or form data.

    REST_FRAMEWORK = {
         'DEFAULT_PARSER_CLASSES': (
    -        'rest_framework.parsers.YAMLParser',
    +        'rest_framework.parsers.JSONParser',
         )
     }
     

    You can also set the parsers used for an individual view, or viewset, using the APIView class based views.

    -
    from rest_framework.parsers import YAMLParser
    +
    from rest_framework.parsers import JSONParser
     from rest_framework.response import Response
     from rest_framework.views import APIView
     
     class ExampleView(APIView):
         """
    -    A view that can accept POST requests with YAML content.
    +    A view that can accept POST requests with JSON content.
         """
    -    parser_classes = (YAMLParser,)
    +    parser_classes = (JSONParser,)
     
         def post(self, request, format=None):
             return Response({'received data': request.data})
     

    Or, if you're using the @api_view decorator with function based views.

    @api_view(['POST'])
    -@parser_classes((YAMLParser,))
    +@parser_classes((JSONParser,))
     def example_view(request, format=None):
         """
    -    A view that can accept POST requests with YAML content.
    +    A view that can accept POST requests with JSON content.
         """
         return Response({'received data': request.data})
     
    @@ -508,16 +500,6 @@ def example_view(request, format=None):

    JSONParser

    Parses JSON request content.

    .media_type: application/json

    -

    YAMLParser

    -

    Parses YAML request content.

    -

    Requires the pyyaml package to be installed.

    -

    .media_type: application/yaml

    -

    XMLParser

    -

    Parses REST framework's default style of XML request content.

    -

    Note that the XML markup language is typically used as the base language for more strictly defined domain-specific languages, such as RSS, Atom, and XHTML.

    -

    If you are considering using XML for your API, you may want to consider implementing a custom renderer and parser for your specific requirements, and using an existing domain-specific media-type, or creating your own custom XML-based media-type.

    -

    Requires the defusedxml package to be installed.

    -

    .media_type: application/xml

    FormParser

    Parses HTML form content. request.data will be populated with a QueryDict of data.

    You will typically want to use both FormParser and MultiPartParser together in order to fully support HTML form data.

    @@ -561,7 +543,7 @@ def example_view(request, format=None):

    Optional. If supplied, this argument will be a dictionary containing any additional context that may be required to parse the request content.

    By default this will include the following keys: view, request, args, kwargs.

    Example

    -

    The following is an example plaintext parser that will populate the request.data property with a string representing the body of the request.

    +

    The following is an example plaintext parser that will populate the request.data property with a string representing the body of the request.

    class PlainTextParser(BaseParser):
     """
     Plain text parser.
    @@ -578,6 +560,38 @@ def parse(self, stream, media_type=None, parser_context=None):
     

    Third party packages

    The following third party packages are also available.

    +

    YAML

    +

    REST framework YAML provides YAML parsing and rendering support. It was previously included directly in the REST framework package, and is now instead supported as a third-party package.

    +

    Installation & configuration

    +

    Install using pip.

    +
    $ pip install djangorestframework-yaml
    +
    +

    Modify your REST framework settings.

    +
    REST_FRAMEWORK = {
    +    'DEFAULT_PARSER_CLASSES': (
    +        'rest_framework_yaml.parsers.YAMLParser',
    +    ),
    +    'DEFAULT_RENDERER_CLASSES': (
    +        'rest_framework_yaml.renderers.YAMLRenderer',
    +    ),
    +}
    +
    +

    XML

    +

    REST Framework XML provides a simple informal XML format. It was previously included directly in the REST framework package, and is now instead supported as a third-party package.

    +

    Installation & configuration

    +

    Install using pip.

    +
    $ pip install djangorestframework-xml
    +
    +

    Modify your REST framework settings.

    +
    REST_FRAMEWORK = {
    +    'DEFAULT_PARSER_CLASSES': (
    +        'rest_framework_xml.parsers.XMLParser',
    +    ),
    +    'DEFAULT_RENDERER_CLASSES': (
    +        'rest_framework_xml.renderers.XMLRenderer',
    +    ),
    +}
    +

    MessagePack

    MessagePack is a fast, efficient binary serialization format. Juan Riaza maintains the djangorestframework-msgpack package which provides MessagePack renderer and parser support for REST framework.

    CamelCase JSON

    -- cgit v1.2.3