diff -r 44a02d6b74e4 Doc/library/urllib.request.rst --- a/Doc/library/urllib.request.rst Thu Jul 21 19:49:47 2011 +0200 +++ b/Doc/library/urllib.request.rst Fri Jul 22 10:47:47 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,7 @@ document, and the user had no option to approve the automatic fetching of the image, this should be true. + *method* The method to use (e.g. GET, POST ...) .. class:: OpenerDirector() diff -r 44a02d6b74e4 Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Thu Jul 21 19:49:47 2011 +0200 +++ b/Lib/test/test_urllib2.py Fri Jul 22 10:47:47 2011 -0700 @@ -1384,6 +1384,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 44a02d6b74e4 Lib/urllib/request.py --- a/Lib/urllib/request.py Thu Jul 21 19:49:47 2011 +0200 +++ b/Lib/urllib/request.py Fri Jul 22 10:47:47 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,6 +203,9 @@ self.host = unquote(self.host) def get_method(self): + if self._method: + return self._method + if self.data is not None: return "POST" else: