rest_framework.parsers

Parsers are used to parse the content of incoming HTTP requests.

They give us a generic way of being able to handle various media types on the request, such as form content or json encoded data.

Module Contents

class DataAndFiles(data, files)

Bases:object

class BaseParser

Bases:object

All parsers should extend BaseParser, specifying a media_type attribute, and overriding the .parse() method.

media_type
parse(self, stream, media_type=None, parser_context=None)

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.

class JSONParser

Bases:rest_framework.parsers.BaseParser

Parses JSON-serialized data.

media_type = application/json
renderer_class
strict
parse(self, stream, media_type=None, parser_context=None)

Parses the incoming bytestream as JSON and returns the resulting data.

class FormParser

Bases:rest_framework.parsers.BaseParser

Parser for form data.

media_type = application/x-www-form-urlencoded
parse(self, stream, media_type=None, parser_context=None)

Parses the incoming bytestream as a URL encoded form, and returns the resulting QueryDict.

class MultiPartParser

Bases:rest_framework.parsers.BaseParser

Parser for multipart form data, which may include file data.

media_type = multipart/form-data
parse(self, stream, media_type=None, parser_context=None)

Parses the incoming bytestream as a multipart encoded form, and returns a DataAndFiles object.

.data will be a QueryDict containing all the form parameters. .files will be a QueryDict containing all the form files.

class FileUploadParser

Bases:rest_framework.parsers.BaseParser

Parser for file upload data.

media_type = */*
errors
parse(self, stream, media_type=None, parser_context=None)

Treats the incoming bytestream as a raw file upload and returns a DataAndFiles object.

.data will be None (we expect request body to be a file content). .files will be a QueryDict containing one ‘file’ element.

get_filename(self, stream, media_type, parser_context)

Detects the uploaded file name. First searches a ‘filename’ url kwarg. Then tries to parse Content-Disposition header.

get_encoded_filename(self, filename_parm)

Handle encoded filenames per RFC6266. See also: https://tools.ietf.org/html/rfc2231#section-4