Commit 3f003a3c authored by Adrian Holovaty's avatar Adrian Holovaty
Browse files

Fixed #17323 -- Renamed HttpRequest.raw_post_data to request.body. Thanks for the patch, dstufft

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17210 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 61f0aff8
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ import datetime
import os
import re
import time
import warnings

from pprint import pformat
from urllib import urlencode, quote
from urlparse import urljoin
@@ -315,14 +317,19 @@ class HttpRequest(object):
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse()

    def _get_raw_post_data(self):
        if not hasattr(self, '_raw_post_data'):
    @property
    def body(self):
        if not hasattr(self, '_body'):
            if self._read_started:
                raise Exception("You cannot access raw_post_data after reading from request's data stream")
            self._raw_post_data = self.read()
            self._stream = StringIO(self._raw_post_data)
        return self._raw_post_data
    raw_post_data = property(_get_raw_post_data)
                raise Exception("You cannot access body after reading from request's data stream")
            self._body = self.read()
            self._stream = StringIO(self._body)
        return self._body

    @property
    def raw_post_data(self):
        warnings.warn('HttpRequest.raw_post_data has been deprecated. Use HttpRequest.body instead.', PendingDeprecationWarning)
        return self.body

    def _mark_post_parse_error(self):
        self._post = QueryDict('')
@@ -334,14 +341,14 @@ class HttpRequest(object):
        if self.method != 'POST':
            self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
            return
        if self._read_started and not hasattr(self, '_raw_post_data'):
        if self._read_started and not hasattr(self, '_body'):
            self._mark_post_parse_error()
            return

        if self.META.get('CONTENT_TYPE', '').startswith('multipart'):
            if hasattr(self, '_raw_post_data'):
            if hasattr(self, '_body'):
                # Use already read data
                data = StringIO(self._raw_post_data)
                data = StringIO(self._body)
            else:
                data = self
            try:
@@ -357,14 +364,14 @@ class HttpRequest(object):
                self._mark_post_parse_error()
                raise
        else:
            self._post, self._files = QueryDict(self.raw_post_data, encoding=self._encoding), MultiValueDict()
            self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()

    ## File-like and iterator interface.
    ##
    ## Expects self._stream to be set to an appropriate source of bytes by
    ## a corresponding request subclass (WSGIRequest or ModPythonRequest).
    ## Also when request data has already been read by request.POST or
    ## request.raw_post_data, self._stream points to a StringIO instance
    ## request.body, self._stream points to a StringIO instance
    ## containing that data.

    def read(self, *args, **kwargs):
+4 −0
Original line number Diff line number Diff line
@@ -257,6 +257,10 @@ these changes.
* Setting the ``is_safe`` and ``needs_autoescape`` flags as attributes of
  template filter functions will no longer be supported.

* The attribute ``HttpRequest.raw_post_data`` was renamed to ``HttpRequest.body``
  in 1.4. The backward compatibility will be removed --
  ``HttpRequest.raw_post_data`` will no longer work.

2.0
---

+14 −11
Original line number Diff line number Diff line
@@ -30,6 +30,20 @@ Attributes

All attributes except ``session`` should be considered read-only.

.. attribute:: HttpRequest.body

    .. versionchanged:: 1.4
    Before Django 1.4, ``HttpRequest.body`` was named ``HttpRequest.raw_post_data``.

    The raw HTTP request body as a byte string. This is useful for processing
    data in different ways than conventional HTML forms: binary images,
    XML payload etc. For processing conventional form data, use ``HttpRequest.POST``.

    .. versionadded:: 1.3

    You can also read from an HttpRequest using a file-like interface. See
    :meth:`HttpRequest.read()`.

.. attribute:: HttpRequest.path

    A string representing the full path to the requested page, not including
@@ -170,17 +184,6 @@ All attributes except ``session`` should be considered read-only.
    support activated. See the :doc:`session documentation
    </topics/http/sessions>` for full details.

.. attribute:: HttpRequest.raw_post_data

    The raw HTTP POST data as a byte string. This is useful for processing
    data in different formats than of conventional HTML forms: binary images,
    XML payload etc. For processing form data use ``HttpRequest.POST``.

    .. versionadded:: 1.3

    You can also read from an HttpRequest using file-like interface. See
    :meth:`HttpRequest.read()`.

.. attribute:: HttpRequest.urlconf

    Not defined by Django itself, but will be read if other code (e.g., a custom
+7 −0
Original line number Diff line number Diff line
@@ -995,3 +995,10 @@ useful, it was removed in Django 1.4. If you relied on it, you must edit your
settings file to list all your applications explicitly.

.. _this can't be done reliably: http://docs.python.org/tutorial/modules.html#importing-from-a-package

``HttpRequest.raw_post_data`` renamed to ``HttpRequest.body``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This attribute was confusingly named ``HttpRequest.raw_post_data``, but it
actually provided the body of the HTTP request. It's been renamed to
``HttpRequest.body``, and ``HttpRequest.raw_post_data`` has been deprecated.
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ def raw_post_view(request):
    """A view which expects raw XML to be posted and returns content extracted
    from the XML"""
    if request.method == 'POST':
        root = parseString(request.raw_post_data)
        root = parseString(request.body)
        first_book = root.firstChild.firstChild
        title, author = [n.firstChild.nodeValue for n in first_book.childNodes]
        t = Template("{{ title }} - {{ author }}", name="Book template")
Loading