aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/parsers.py
diff options
context:
space:
mode:
authorTom Christie2013-02-04 21:16:34 +0000
committerTom Christie2013-02-04 21:16:34 +0000
commit0a38bc9db8c7ad5c1a9c8429ac799260c7257a39 (patch)
tree1f9b0d797f5f3fbc6bcdaaeb8e5f95c9eab28669 /rest_framework/parsers.py
parentb052c92ac38f90e5b56cfd128cd4a488713c048e (diff)
downloaddjango-rest-framework-0a38bc9db8c7ad5c1a9c8429ac799260c7257a39.tar.bz2
Deal with parser encodings properly
Diffstat (limited to 'rest_framework/parsers.py')
-rw-r--r--rest_framework/parsers.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py
index 26009cdf..98d63fec 100644
--- a/rest_framework/parsers.py
+++ b/rest_framework/parsers.py
@@ -5,6 +5,7 @@ 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.
"""
from __future__ import unicode_literals
+from django.conf import settings
from django.http import QueryDict
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
from django.http.multipartparser import MultiPartParserError
@@ -55,8 +56,11 @@ class JSONParser(BaseParser):
`data` will be an object which is the parsed content of the response.
`files` will always be `None`.
"""
+ parser_context = parser_context or {}
+ encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
+
try:
- data = stream.read().decode('iso-8859-1')
+ data = stream.read().decode(encoding)
return json.loads(data)
except ValueError as exc:
raise ParseError('JSON parse error - %s' % six.text_type(exc))
@@ -76,8 +80,11 @@ class YAMLParser(BaseParser):
`data` will be an object which is the parsed content of the response.
`files` will always be `None`.
"""
+ parser_context = parser_context or {}
+ encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
+
try:
- data = stream.read().decode('iso-8859-1')
+ data = stream.read().decode(encoding)
return yaml.safe_load(data)
except (ValueError, yaml.parser.ParserError) as exc:
raise ParseError('YAML parse error - %s' % six.u(exc))
@@ -97,7 +104,9 @@ class FormParser(BaseParser):
`data` will be a :class:`QueryDict` containing all the form parameters.
`files` will always be :const:`None`.
"""
- data = QueryDict(stream.read())
+ parser_context = parser_context or {}
+ encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
+ data = QueryDict(stream.read(), encoding=encoding)
return data
@@ -117,11 +126,12 @@ class MultiPartParser(BaseParser):
"""
parser_context = parser_context or {}
request = parser_context['request']
+ encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
meta = request.META
upload_handlers = request.upload_handlers
try:
- parser = DjangoMultiPartParser(meta, stream, upload_handlers)
+ parser = DjangoMultiPartParser(meta, stream, upload_handlers, encoding)
data, files = parser.parse()
return DataAndFiles(data, files)
except MultiPartParserError as exc:
@@ -136,8 +146,11 @@ class XMLParser(BaseParser):
media_type = 'application/xml'
def parse(self, stream, media_type=None, parser_context=None):
+ parser_context = parser_context or {}
+ encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
+ parser = ET.XMLParser(encoding=encoding)
try:
- tree = ET.parse(stream)
+ tree = ET.parse(stream, parser=parser)
except (ExpatError, ETParseError, ValueError) as exc:
raise ParseError('XML parse error - %s' % six.u(exc))
data = self._xml_convert(tree.getroot())