From ed93e13a1c6f792e14176bdaa5e96d0fa2c63a2f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Dec 2014 12:20:07 +0000 Subject: Update documentation --- api-guide/parsers/index.html | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'api-guide/parsers/index.html') diff --git a/api-guide/parsers/index.html b/api-guide/parsers/index.html index 5a026c0e..b4306579 100644 --- a/api-guide/parsers/index.html +++ b/api-guide/parsers/index.html @@ -164,6 +164,10 @@ Serializer relations +
REST framework includes a number of built in Parser classes, that allow you to accept requests with various media types. There is also support for defining your own custom parsers, which gives you the flexibility to design the media types that your API accepts.
The set of valid parsers for a view is always defined as a list of classes. When either request.DATA or request.FILES is accessed, REST framework will examine the Content-Type header on the incoming request, and determine which parser to use to parse the request content.
The set of valid parsers for a view is always defined as a list of classes. When request.data is accessed, REST framework will examine the Content-Type header on the incoming request, and determine which parser to use to parse the request content.
Note: When developing client applications always remember to make sure you're setting the Content-Type header when sending data in an HTTP request.
If you don't set the content type, most clients will default to using 'application/x-www-form-urlencoded', which may not be what you wanted.
Or, if you're using the @api_view decorator with function based views.
@api_view(['POST'])
@@ -485,7 +493,7 @@ def example_view(request, format=None):
"""
A view that can accept POST requests with YAML content.
"""
- return Response({'received data': request.DATA})
+ return Response({'received data': request.data})
Requires the defusedxml package to be installed.
.media_type: application/xml
Parses HTML form content. request.DATA will be populated with a QueryDict of data, request.FILES will be populated with an empty QueryDict of data.
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.
.media_type: application/x-www-form-urlencoded
Parses multipart HTML form content, which supports file uploads. Both request.DATA and request.FILES will be populated with a QueryDict.
Parses multipart HTML form content, which supports file uploads. Both request.data will be populated with a QueryDict.
You will typically want to use both FormParser and MultiPartParser together in order to fully support HTML form data.
.media_type: multipart/form-data
Parses raw file upload content. The request.DATA property will be an empty QueryDict, and request.FILES will be a dictionary with a single key 'file' containing the uploaded file.
Parses raw file upload content. The request.data property will be a dictionary with a single key 'file' containing the uploaded file.
If the view used with FileUploadParser is called with a filename URL keyword argument, then that argument will be used as the filename. If it is called without a filename URL keyword argument, then the client must set the filename in the Content-Disposition HTTP header. For example Content-Disposition: attachment; filename=upload.jpg.
.media_type: */*
To implement a custom parser, you should override BaseParser, set the .media_type property, and implement the .parse(self, stream, media_type, parser_context) method.
The method should return the data that will be used to populate the request.DATA property.
The method should return the data that will be used to populate the request.data property.
The arguments passed to .parse() are:
A stream-like object representing the body of the request.
@@ -545,7 +553,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.
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.
@@ -580,7 +588,7 @@ def parse(self, stream, media_type=None, parser_context=None):
--
cgit v1.2.3