From 664021a9b7fcfe4e2350387c19356b5895842962 Mon Sep 17 00:00:00 2001 From: Chris McKenzie Date: Mon, 4 Aug 2014 10:49:34 -0400 Subject: [PATCH 1/2] add content_type query param override --- rest_framework/request.py | 13 +++++++++++-- rest_framework/settings.py | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rest_framework/request.py b/rest_framework/request.py index 40467c03d..3fb60d806 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -116,6 +116,7 @@ class Request(object): _METHOD_PARAM = api_settings.FORM_METHOD_OVERRIDE _CONTENT_PARAM = api_settings.FORM_CONTENT_OVERRIDE _CONTENTTYPE_PARAM = api_settings.FORM_CONTENTTYPE_OVERRIDE + _URL_CONTENTTYPE_PARAM = api_settings.URL_CONTENTTYPE_OVERRIDE def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): @@ -271,8 +272,9 @@ class Request(object): Sets the method and content_type, and then check if they've been overridden. """ - self._content_type = self.META.get('HTTP_CONTENT_TYPE', - self.META.get('CONTENT_TYPE', '')) + self._content_type = self.QUERY_PARAMS.get( + self._URL_CONTENTTYPE_PARAM, self.META.get('HTTP_CONTENT_TYPE', + self.META.get('CONTENT_TYPE', ''))) self._perform_form_overloading() @@ -336,6 +338,13 @@ class Request(object): self._stream = BytesIO(self._data[self._CONTENT_PARAM].encode(self.parser_context['encoding'])) self._data, self._files = (Empty, Empty) + if 'content_type' in self.QUERY_PARAMS and \ + self.QUERY_PARAMS.get('content_type').startswith( + 'application/x-www-form-urlencoded'): + self._stream = BytesIO(self._request.body.encode( + self.parser_context['encoding'])) + self._data, self._files = (Empty, Empty) + def _parse(self): """ Parse the request content, returning a two-tuple of (data, files) diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 38753c968..f15f7f39f 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -97,6 +97,7 @@ DEFAULTS = { 'FORM_CONTENTTYPE_OVERRIDE': '_content_type', 'URL_ACCEPT_OVERRIDE': 'accept', 'URL_FORMAT_OVERRIDE': 'format', + 'URL_CONTENTTYPE_OVERRIDE': 'content_type', 'FORMAT_SUFFIX_KWARG': 'format', 'URL_FIELD_NAME': 'url', From b06a1061f03dae03e79ea74129093b09698faf83 Mon Sep 17 00:00:00 2001 From: Chris McKenzie Date: Mon, 4 Aug 2014 10:50:12 -0400 Subject: [PATCH 2/2] test content_type query param override --- rest_framework/tests/test_request.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rest_framework/tests/test_request.py b/rest_framework/tests/test_request.py index c0b50f330..90e649e7f 100644 --- a/rest_framework/tests/test_request.py +++ b/rest_framework/tests/test_request.py @@ -71,6 +71,10 @@ class TestMethodOverloading(TestCase): request = Request(factory.get('/', {'foo': 'bar'}, HTTP_X_HTTP_METHOD_OVERRIDE='DELETE')) self.assertEqual(request.method, 'DELETE') + def test_content_type_override_query(self): + request = Request(factory.post('/?content_type=application/x-www-form-urlencoded', HTTP_CONTENT_TYPE='text/plain'), {'email': 'mmmmmm@test.com'}) + self.assertEqual(request.content_type, 'application/x-www-form-urlencoded') + class TestContentParsing(TestCase): def test_standard_behaviour_determines_no_content_GET(self):