diff -r 683a9aeb5d58 -r fbf2f56d225f Doc/library/urllib.request.rst --- a/Doc/library/urllib.request.rst Wed Jul 27 01:22:41 2011 +0200 +++ b/Doc/library/urllib.request.rst Tue Jul 26 16:53:54 2011 -0700 @@ -132,7 +132,8 @@ The following classes are provided: -.. class:: Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False) +.. class:: Request(url, data=None, headers={}, origin_req_host=None, \ + unverifiable=False, method=None) This class is an abstraction of a URL request. @@ -157,7 +158,7 @@ :mod:`urllib`'s default user agent string is ``"Python-urllib/2.6"`` (on Python 2.6). - The final two arguments are only of interest for correct handling + The following two arguments are only of interest for correct handling of third-party HTTP cookies: *origin_req_host* should be the request-host of the origin @@ -175,6 +176,9 @@ document, and the user had no option to approve the automatic fetching of the image, this should be true. + *method* is the HTTP method to use (e.g. ``'GET'``, ``'POST'`` ...), if + *method* is ``None``, then the method will be ``'POST'`` is *data* is not + ``None``, otherwise ``'GET'`` (see :meth:`get_method`). .. class:: OpenerDirector() @@ -379,7 +383,7 @@ .. method:: Request.get_method() Return a string indicating the HTTP request method. This is only meaningful for - HTTP requests, and currently always returns ``'GET'`` or ``'POST'``. + HTTP requests. .. method:: Request.has_data() diff -r 683a9aeb5d58 -r fbf2f56d225f Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Wed Jul 27 01:22:41 2011 +0200 +++ b/Lib/test/test_urllib2.py Tue Jul 26 16:53:54 2011 -0700 @@ -1385,6 +1385,13 @@ self.assertEqual("POST", self.post.get_method()) self.assertEqual("GET", self.get.get_method()) + # Test explicit method + req = Request("http://python.org", method="POST") + self.assertEqual("POST", req.get_method()) + + req = Request("http://python.org", data="data", method="PUT") + self.assertEqual("PUT", req.get_method()) + def test_add_data(self): self.assertFalse(self.get.has_data()) self.assertEqual("GET", self.get.get_method()) diff -r 683a9aeb5d58 -r fbf2f56d225f Lib/urllib/request.py --- a/Lib/urllib/request.py Wed Jul 27 01:22:41 2011 +0200 +++ b/Lib/urllib/request.py Tue Jul 26 16:53:54 2011 -0700 @@ -177,7 +177,7 @@ class Request: def __init__(self, url, data=None, headers={}, - origin_req_host=None, unverifiable=False): + origin_req_host=None, unverifiable=False, method=None): # unwrap('') --> 'type://host/path' self.full_url = unwrap(url) self.full_url, self.fragment = splittag(self.full_url) @@ -192,6 +192,7 @@ self.origin_req_host = origin_req_host self.unverifiable = unverifiable self._parse() + self._method = method def _parse(self): self.type, rest = splittype(self.full_url) @@ -202,10 +203,10 @@ self.host = unquote(self.host) def get_method(self): - if self.data is not None: - return "POST" - else: - return "GET" + if self._method: + return self._method + + return "GET" if self.data is None else "POST" # Begin deprecated methods