>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>
-OAuth Authentication
---------------------
+Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for
+this authentication method::
+
+ >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
+ <Response [200]>
+
+Providing the credentials as a tuple in this fashion is functionally equivalent
+to the ``HTTPBasicAuth`` example above.
-Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for
-establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.
Digest Authentication
---------------------
<Response [200]>
+OAuth Authentication
+--------------------
+
+Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for
+establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.
+
+
Redirection and History
-----------------------
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
+from .auth import HTTPBasicAuth
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
#: content and metadata of HTTP Response, once :attr:`sent <send>`.
self.response = Response()
- #: Authentication tuple to attach to :class:`Request <Request>`.
+ #: Authentication tuple or object to attach to :class:`Request <Request>`.
self.auth = auth
#: CookieJar to attach to :class:`Request <Request>`.
if (content_type) and (not 'content-type' in self.headers):
self.headers['Content-Type'] = content_type
-
if self.auth:
+ if isinstance(self.auth, tuple) and len(self.auth) == 2:
+ # special-case basic HTTP auth
+ self.auth = HTTPBasicAuth(*self.auth)
+
# Allow auth to make its changes.
r = self.auth(self)
self.assertEqual(r.status_code, 200)
+ def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):
+
+ for service in SERVICES:
+
+ auth = ('user', 'pass')
+ url = service('basic-auth', 'user', 'pass')
+
+ r = requests.get(url, auth=auth)
+ self.assertEqual(r.status_code, 200)
+
+ r = requests.get(url)
+ self.assertEqual(r.status_code, 401)
+
+
+ s = requests.session(auth=auth)
+ r = s.get(url)
+ self.assertEqual(r.status_code, 200)
+
+
def test_BASICAUTH_HTTP_200_OK_GET(self):
for service in SERVICES: