Ticket #17360: documenting_restful_testing.diff

File documenting_restful_testing.diff, 1.7 KB (added by thibaultj, 13 years ago)

Same patch as is the pull request

  • docs/topics/testing.txt

    diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
    index 181b4ff..40413a3 100644
    a b arguments at time of construction:  
    846846
    847847        The ``extra`` argument acts the same as for :meth:`Client.get`.
    848848
     849    .. admonition:: Testing RESTful web services
     850
     851       Since web browsers are only designed to perform **GET** and **POST**
     852       requests, the :attr:`~django.http.HttpRequest.POST` variable won't be accessible if you
     853       performed a request with a http verb that is not **POST**, even if the
     854       request body is url encoded.
     855
     856       To access the submitted data, you need to use the
     857       :attr:`~django.http.HttpRequest.raw_post_data`, which contains the
     858       raw body of the HTTP request.
     859
     860       If you know the incoming request will be **x-www-form-urlencoded** or
     861       **multipart/form-data** encoded, you can use the following method::
     862
     863            from cStringIO import StringIO
     864            from django.http import QueryDict
     865            from django.http.multipartparser import MultiPartParser
     866
     867
     868            def parse_put_data(request):
     869                content_type = request.META.get('CONTENT_TYPE')
     870                if content_type.startswith('multipart'):
     871                    parser = MultiPartParser(request.META, StringIO(request.raw_post_data), [], request.encoding)
     872                    post, files = parser.parse()
     873                else:
     874                    post = QueryDict(request.raw_post_data)
     875                return post
     876
     877
     878
    849879    .. method:: Client.login(**credentials)
    850880
    851881        If your site uses Django's :doc:`authentication system</topics/auth>`
Back to Top