diff --git a/django/http/__init__.py b/django/http/__init__.py
index ecb3912..0b13857 100644
a
|
b
|
class HttpRequest(object):
|
310 | 310 | self._post_parse_error = True |
311 | 311 | |
312 | 312 | def _load_post_and_files(self): |
313 | | # Populates self._post and self._files |
| 313 | """ Populates self._post and self._files if the content-type is a form type """ |
314 | 314 | if self.method != 'POST': |
315 | 315 | self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict() |
316 | 316 | return |
… |
… |
class HttpRequest(object):
|
318 | 318 | self._mark_post_parse_error() |
319 | 319 | return |
320 | 320 | |
321 | | if self.META.get('CONTENT_TYPE', '').startswith('multipart'): |
| 321 | if self.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'): |
322 | 322 | if hasattr(self, '_body'): |
323 | 323 | # Use already read data |
324 | 324 | data = BytesIO(self._body) |
… |
… |
class HttpRequest(object):
|
336 | 336 | # empty POST |
337 | 337 | self._mark_post_parse_error() |
338 | 338 | raise |
339 | | else: |
| 339 | elif self.META.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'): |
340 | 340 | self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict() |
| 341 | else: |
| 342 | self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict() |
341 | 343 | |
342 | 344 | ## File-like and iterator interface. |
343 | 345 | ## |
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index 90872a6..643dc1a 100644
a
|
b
|
All attributes should be considered read-only, unless stated otherwise below.
|
92 | 92 | |
93 | 93 | .. attribute:: HttpRequest.POST |
94 | 94 | |
95 | | A dictionary-like object containing all given HTTP POST parameters. See the |
| 95 | A dictionary-like object containing all given HTTP POST parameters, |
| 96 | providing that the request contains form data. See the |
96 | 97 | :class:`QueryDict` documentation below. |
97 | 98 | |
| 99 | .. versionchanged:: 1.5 |
| 100 | Before Django 1.5, HttpRequest.POST did also contain non-form data. Now if |
| 101 | the ``Content-Type`` header is not ``multipart/form-data`` or |
| 102 | ``application/x-www-form-urlencoded``, you should access the posted data |
| 103 | through :attr:`HttpRequest.body` instead. |
| 104 | |
98 | 105 | It's possible that a request can come in via POST with an empty ``POST`` |
99 | 106 | dictionary -- if, say, a form is requested via the POST HTTP method but |
100 | 107 | does not include form data. Therefore, you shouldn't use ``if request.POST`` |
diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt
index d49bae8..b60761d 100644
a
|
b
|
For consistency with the design of the other generic views,
|
245 | 245 | dictionary into the context, instead passing the variables from the URLconf |
246 | 246 | directly into the context. |
247 | 247 | |
| 248 | Non-form data in HTTP requests |
| 249 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 250 | |
| 251 | :attr:`request.POST <django.http.HttpRequest.POST>` will no longer include data |
| 252 | posted via HTTP requests with non form-specific content-types in the header. |
| 253 | In prior versions, data posted with content-types other than |
| 254 | ``multipart/form-data`` or ``application/x-www-form-urlencoded`` would still |
| 255 | end up represented in the :attr:`request.POST <django.http.HttpRequest.POST>` |
| 256 | attribute. Developers wishing to access the raw POST data for these cases, |
| 257 | should use the :attr:`request.body <django.http.HttpRequest.body>` attribute |
| 258 | instead. |
| 259 | |
| 260 | For requests without a ``Content-Type`` header, this conforms to the HTTP |
| 261 | specification (:rfc:`2616`) which states that: *If the media type remains |
| 262 | unknown, the recipient SHOULD treat it as type "application/octet-stream"*. |
| 263 | |
248 | 264 | OPTIONS, PUT and DELETE requests in the test client |
249 | 265 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
250 | 266 | |
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
index f9e1112..fc26844 100644
a
|
b
|
class RequestsTests(unittest.TestCase):
|
265 | 265 | def test_stream(self): |
266 | 266 | payload = b'name=value' |
267 | 267 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 268 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
268 | 269 | 'CONTENT_LENGTH': len(payload), |
269 | 270 | 'wsgi.input': BytesIO(payload)}) |
270 | 271 | self.assertEqual(request.read(), b'name=value') |
… |
… |
class RequestsTests(unittest.TestCase):
|
276 | 277 | """ |
277 | 278 | payload = b'name=value' |
278 | 279 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 280 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
279 | 281 | 'CONTENT_LENGTH': len(payload), |
280 | 282 | 'wsgi.input': BytesIO(payload)}) |
281 | 283 | self.assertEqual(request.POST, {'name': ['value']}) |
… |
… |
class RequestsTests(unittest.TestCase):
|
289 | 291 | """ |
290 | 292 | payload = b'name=value' |
291 | 293 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 294 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
292 | 295 | 'CONTENT_LENGTH': len(payload), |
293 | 296 | 'wsgi.input': BytesIO(payload)}) |
294 | 297 | self.assertEqual(request.read(2), b'na') |
… |
… |
class RequestsTests(unittest.TestCase):
|
337 | 340 | 'wsgi.input': BytesIO(payload)}) |
338 | 341 | self.assertEqual(request.POST, {}) |
339 | 342 | |
| 343 | def test_POST_binary_only(self): |
| 344 | payload = b'\r\n\x01\x00\x00\x00ab\x00\x00\xcd\xcc,@' |
| 345 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 346 | 'CONTENT_TYPE': 'application/octet-stream', |
| 347 | 'CONTENT_LENGTH': len(payload), |
| 348 | 'wsgi.input': BytesIO(payload)}) |
| 349 | self.assertEqual(request.POST, {}) |
| 350 | self.assertEqual(request.FILES, {}) |
| 351 | self.assertEqual(request.body, payload) |
| 352 | |
340 | 353 | def test_read_by_lines(self): |
341 | 354 | payload = b'name=value' |
342 | 355 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 356 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
343 | 357 | 'CONTENT_LENGTH': len(payload), |
344 | 358 | 'wsgi.input': BytesIO(payload)}) |
345 | 359 | self.assertEqual(list(request), [b'name=value']) |
… |
… |
class RequestsTests(unittest.TestCase):
|
350 | 364 | """ |
351 | 365 | payload = b'name=value' |
352 | 366 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 367 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
353 | 368 | 'CONTENT_LENGTH': len(payload), |
354 | 369 | 'wsgi.input': BytesIO(payload)}) |
355 | 370 | raw_data = request.body |
… |
… |
class RequestsTests(unittest.TestCase):
|
362 | 377 | """ |
363 | 378 | payload = b'name=value' |
364 | 379 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 380 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
365 | 381 | 'CONTENT_LENGTH': len(payload), |
366 | 382 | 'wsgi.input': BytesIO(payload)}) |
367 | 383 | raw_data = request.body |
… |
… |
class RequestsTests(unittest.TestCase):
|
414 | 430 | |
415 | 431 | payload = b'name=value' |
416 | 432 | request = WSGIRequest({'REQUEST_METHOD': 'POST', |
| 433 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
417 | 434 | 'CONTENT_LENGTH': len(payload), |
418 | 435 | 'wsgi.input': ExplodingBytesIO(payload)}) |
419 | 436 | |