Skip to content
Myr

Added support for HTTP Basic Auth credentials in 2-tuple

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Idan Gazit · committed by Idan Gazit · parent 7fdb09b · Browse files · 3 files+40 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+40 added−6 removed

  • other:+40 added−6 removed

3 files+40 added−6 removed

modified:docs/user/quickstart.rst+15 added-4 removed
237 unmodified lines
238 >>> from requests.auth import HTTPBasicAuth239 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))240 <Response [200]>241242OAuth Authentication243--------------------242Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for243this authentication method::244245Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for246establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.245 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))246 <Response [200]>247248Providing the credentials as a tuple in this fashion is functionally equivalent249to the ``HTTPBasicAuth`` example above.250251252Digest Authentication253---------------------254255Another popular form of web service protection is Digest Authentication::
3 unmodified lines
259 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))260 <Response [200]>261262263OAuth Authentication264--------------------265266Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for267establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.268269270Redirection and History271-----------------------272273Requests will automatically perform location redirection while using impodotent methods.
66 unmodified lines
modified:requests/models.py+6 added-2 removed
15 unmodified lines
1617from .hooks import dispatch_hook18from .structures import CaseInsensitiveDict19from .status_codes import codes20from .auth import HTTPBasicAuth21from .packages.urllib3.exceptions import MaxRetryError22from .packages.urllib3.exceptions import SSLError as _SSLError23from .packages.urllib3.exceptions import HTTPError as _HTTPError24from .packages.urllib3 import connectionpool, poolmanager
72 unmodified lines
97 #: :class:`Response <Response>` instance, containing98 #: content and metadata of HTTP Response, once :attr:`sent <send>`.99 self.response = Response()100100 #: Authentication tuple to attach to :class:`Request <Request>`.101 #: Authentication tuple or object to attach to :class:`Request <Request>`.102 self.auth = auth103104 #: CookieJar to attach to :class:`Request <Request>`.105 self.cookies = dict(cookies or [])
282 unmodified lines
388 # Add content-type if it wasn't explicitly provided.389 if (content_type) and (not 'content-type' in self.headers):390 self.headers['Content-Type'] = content_type391391392 if self.auth:393 if isinstance(self.auth, tuple) and len(self.auth) == 2:394 # special-case basic HTTP auth395 self.auth = HTTPBasicAuth(*self.auth)396397 # Allow auth to make its changes.398 r = self.auth(self)399400 # Update self to reflect the auth changes.
218 unmodified lines
modified:test_requests.py+19 added
139 unmodified lines
140 r = requests.patch(httpbin('patch'))141 self.assertEqual(r.status_code, 200)142143144 def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):145146 for service in SERVICES:147148 auth = ('user', 'pass')149 url = service('basic-auth', 'user', 'pass')150151 r = requests.get(url, auth=auth)152 self.assertEqual(r.status_code, 200)153154 r = requests.get(url)155 self.assertEqual(r.status_code, 401)156157158 s = requests.session(auth=auth)159 r = s.get(url)160 self.assertEqual(r.status_code, 200)161162163 def test_BASICAUTH_HTTP_200_OK_GET(self):164165 for service in SERVICES:166
412 unmodified lines