mirror of
https://github.com/django/django.git
synced 2025-09-17 22:49:35 +00:00
Allowed json parser to parse request or bytes.
This commit is contained in:
parent
113c169142
commit
8eb9dab0ca
@ -16,16 +16,11 @@ class BaseParser:
|
|||||||
def parse(self, data, request=None):
|
def parse(self, data, request=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@property
|
|
||||||
def _supports_form_parsing(self):
|
|
||||||
form_media = ("application/x-www-form-urlencoded", "multipart/form-data")
|
|
||||||
return self.media_type in form_media
|
|
||||||
|
|
||||||
|
|
||||||
class FormParser(BaseParser):
|
class FormParser(BaseParser):
|
||||||
media_type = "application/x-www-form-urlencoded"
|
media_type = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
def parse(self, data, request=None):
|
def parse(self, request):
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
|
|
||||||
# According to RFC 1866, the "application/x-www-form-urlencoded"
|
# According to RFC 1866, the "application/x-www-form-urlencoded"
|
||||||
@ -42,7 +37,7 @@ class FormParser(BaseParser):
|
|||||||
class MultiPartParser(BaseParser):
|
class MultiPartParser(BaseParser):
|
||||||
media_type = "multipart/form-data"
|
media_type = "multipart/form-data"
|
||||||
|
|
||||||
def parse(self, data, request=None):
|
def parse(self, request):
|
||||||
if hasattr(request, "_body"):
|
if hasattr(request, "_body"):
|
||||||
# Use already read data
|
# Use already read data
|
||||||
data = BytesIO(request._body)
|
data = BytesIO(request._body)
|
||||||
@ -71,6 +66,10 @@ class MultiPartParser(BaseParser):
|
|||||||
class JSONParser(BaseParser):
|
class JSONParser(BaseParser):
|
||||||
media_type = "application/json"
|
media_type = "application/json"
|
||||||
|
|
||||||
def parse(self, data, request=None):
|
# TODO rename request -- it's not always one.
|
||||||
# TODO enable strict mode. Like DRF.
|
def parse(self, request):
|
||||||
return json.loads(data), MultiValueDict()
|
from django.http import HttpRequest
|
||||||
|
|
||||||
|
if isinstance(request, HttpRequest):
|
||||||
|
request = request.body
|
||||||
|
return json.loads(request), MultiValueDict()
|
||||||
|
@ -376,13 +376,8 @@ class HttpRequest:
|
|||||||
if selected_parser:
|
if selected_parser:
|
||||||
selected_parser.parsers = parser_list
|
selected_parser.parsers = parser_list
|
||||||
try:
|
try:
|
||||||
if selected_parser._supports_form_parsing:
|
data, self._files = parser.parse(self)
|
||||||
# TODO Not sure how to make these consistent.
|
setattr(self, data_attr, data)
|
||||||
data, self._files = parser.parse(None, self)
|
|
||||||
setattr(self, data_attr, data)
|
|
||||||
else:
|
|
||||||
data, self._files = parser.parse(self.body, self)
|
|
||||||
setattr(self, data_attr, data)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# TODO 'application/x-www-form-urlencoded' didn't do this.
|
# TODO 'application/x-www-form-urlencoded' didn't do this.
|
||||||
# An error occurred while parsing POST data. Since when
|
# An error occurred while parsing POST data. Since when
|
||||||
|
Loading…
x
Reference in New Issue
Block a user