How to use the djangorestframework.parsers.BaseParser function in djangorestframework

To help you get started, we’ve selected a few djangorestframework examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
if isinstance(string_or_stream, basestring):
            stream = BytesIO(string_or_stream)
        else:
            stream = string_or_stream
        return self.parse_stream(stream, **opts)

    def parse_stream(self, stream, **opts):
        """
        Given a *stream* to read from, return the deserialized output.
        Should return parsed data, or a DataAndFiles object consisting of the
        parsed data and files.
        """
        raise NotImplementedError(".parse_stream() must be overridden.")


class JSONParser(BaseParser):
    """
    Parses JSON-serialized data.
    """

    media_type = 'application/json'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return json.load(stream)
        except ValueError, exc:
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
"""

    media_type = 'application/x-www-form-urlencoded'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will always be :const:`None`.
        """
        data = QueryDict(stream.read())
        return (data, None)


class MultiPartParser(BaseParser):
    """
    Parser for multipart form data, which may include file data.
    """

    media_type = 'multipart/form-data'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will be a :class:`QueryDict` containing all the form files.
        """
        upload_handlers = self.view.request._get_upload_handlers()
        try:
            django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers)
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
Plain text parser.
    """

    media_type = 'text/plain'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will simply be a string representing the body of the request.
        `files` will always be `None`.
        """
        return (stream.read(), None)


class FormParser(BaseParser):
    """
    Parser for form data.
    """

    media_type = 'application/x-www-form-urlencoded'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will always be :const:`None`.
        """
        data = QueryDict(stream.read())
        return (data, None)
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
"""

    media_type = 'application/x-www-form-urlencoded'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will always be :const:`None`.
        """
        data = QueryDict(stream.read())
        return data


class MultiPartParser(BaseParser):
    """
    Parser for multipart form data, which may include file data.
    """

    media_type = 'multipart/form-data'

    def parse_stream(self, stream, **opts):
        """
        Returns a DataAndFiles object.

        `.data` will be a `QueryDict` containing all the form parameters.
        `.files` will be a `QueryDict` containing all the form files.
        """
        meta = opts['meta']
        upload_handlers = opts['upload_handlers']
        try:
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return (json.load(stream), None)
        except ValueError, exc:
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'detail': 'JSON parse error - %s' % unicode(exc)})


if yaml:
    class YAMLParser(BaseParser):
        """
        Parses YAML-serialized data.
        """

        media_type = 'application/yaml'

        def parse(self, stream):
            """
            Returns a 2-tuple of `(data, files)`.

            `data` will be an object which is the parsed content of the response.
            `files` will always be `None`.
            """
            try:
                return (yaml.safe_load(stream), None)
            except ValueError, exc:
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
media_type = 'application/yaml'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return yaml.safe_load(stream)
        except (ValueError, yaml.parser.ParserError), exc:
            raise ParseError('YAML parse error - %s' % unicode(exc))


class PlainTextParser(BaseParser):
    """
    Plain text parser.
    """

    media_type = 'text/plain'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will simply be a string representing the body of the request.
        `files` will always be `None`.
        """
        return stream.read()
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
media_type = 'application/json'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return json.load(stream)
        except ValueError, exc:
            raise ParseError('JSON parse error - %s' % unicode(exc))


class YAMLParser(BaseParser):
    """
    Parses YAML-serialized data.
    """

    media_type = 'application/yaml'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return yaml.safe_load(stream)
        except (ValueError, yaml.parser.ParserError), exc:
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
they match.

        This may be overridden to provide for other behavior, but typically you'll
        instead want to just set the :attr:`media_type` attribute on the class.
        """
        return media_type_matches(self.media_type, content_type)

    def parse(self, stream):
        """
        Given a *stream* to read from, return the deserialized output.
        Should return a 2-tuple of (data, files).
        """
        raise NotImplementedError("BaseParser.parse() Must be overridden to be implemented.")


class JSONParser(BaseParser):
    """
    Parses JSON-serialized data.
    """

    media_type = 'application/json'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be an object which is the parsed content of the response.
        `files` will always be `None`.
        """
        try:
            return (json.load(stream), None)
        except ValueError, exc:
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
"""
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will be a :class:`QueryDict` containing all the form files.
        """
        upload_handlers = self.view.request._get_upload_handlers()
        try:
            django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers)
        except MultiPartParserError, exc:
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'detail': 'multipart parse error - %s' % unicode(exc)})
        return django_parser.parse()
		
		
class XMLParser(BaseParser):
    """
    XML parser.
    """

    media_type = 'application/xml'

    def parse(self, stream):
        """
        Returns a 2-tuple of `(data, files)`.
		
        `data` will simply be a string representing the body of the request.
        `files` will always be `None`.
        """
        data = {}
        tree = ET.parse(stream)
        data = self._xml_convert(tree.getroot())
github encode / django-rest-framework / djangorestframework / parsers.py View on Github external
Plain text parser.
    """

    media_type = 'text/plain'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will simply be a string representing the body of the request.
        `files` will always be `None`.
        """
        return stream.read()


class FormParser(BaseParser):
    """
    Parser for form data.
    """

    media_type = 'application/x-www-form-urlencoded'

    def parse_stream(self, stream, **opts):
        """
        Returns a 2-tuple of `(data, files)`.

        `data` will be a :class:`QueryDict` containing all the form parameters.
        `files` will always be :const:`None`.
        """
        data = QueryDict(stream.read())
        return data