How the parser is determined
The set of valid parsers for a view is always defined as a list of classes. Whenrequest.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.
When developing client applications, always remember to set 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'.Setting the parsers
The default set of parsers may be set globally, using theDEFAULT_PARSER_CLASSES setting:
APIView class-based views:
@api_view decorator with function based views:
Built-in Parsers
BaseParser
rest_framework.parsers.BaseParser
All parsers should extend BaseParser, specifying a media_type attribute, and overriding the .parse() method.
Attributes
The media type that this parser handles (e.g.,
'application/json')Methods
Given a stream to read from, return the parsed representation. Should return parsed data, or a
DataAndFiles object consisting of the parsed data and files.Arguments:stream- A stream-like object representing the body of the requestmedia_type- Optional. The media type of the incoming request contentparser_context- Optional. A dictionary containing additional context (includesview,request,args,kwargsby default)
JSONParser
rest_framework.parsers.JSONParser
Parses JSON-serialized data. request.data will be populated with a dictionary of data.
Attributes
The media type handled by this parser
The corresponding renderer class
Whether to use strict JSON parsing (controlled by the
STRICT_JSON setting)Example
FormParser
rest_framework.parsers.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.
Attributes
The media type handled by this parser
Example
MultiPartParser
rest_framework.parsers.MultiPartParser
Parses multipart HTML form content, which supports file uploads. Both request.data and request.FILES will be populated with QueryDict instances.
You will typically want to use both FormParser and MultiPartParser together in order to fully support HTML form data.
Attributes
The media type handled by this parser
Example
FileUploadParser
rest_framework.parsers.FileUploadParser
Parses raw file upload content. The request.data property will be empty, and request.FILES will contain a single key 'file' with 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. Otherwise, the client must set the filename in the Content-Disposition HTTP header (e.g., Content-Disposition: attachment; filename=upload.jpg).
Attributes
Matches any content type
Error messages dictionary with keys:
'unhandled'- None of the upload handlers can handle the stream'no_filename'- Missing filename in Content-Disposition header
Example
Custom Parsers
To implement a custom parser, you should overrideBaseParser, 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.
Arguments
A stream-like object representing the body of the request
Optional. If provided, this is the media type of the incoming request content. Depending on the request’s
Content-Type: header, this may be more specific than the renderer’s media_type attribute, and may include media type parameters (e.g., "text/plain; charset=utf-8").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 therequest.data property with a string representing the body of the request:
