Skip to content
Myr

Explain custom auth handlers

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 8f8dbec · Browse files · 2 files+34 added−1 removed
This commit

State

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

Diff stats

2 files+34 added−1 removed

  • other:+34 added−1 removed

2 files+34 added−1 removed

modified:docs/user/advanced.rst+33 added
155 unmodified lines
156 }157 }158159160Custom Authentication161---------------------162163Requests allows you to use specify your own authentication mechanism.164165When you pass our authentication tuple to a request method, the first166string is the type of authentication. 'basic' is inferred if none is167provided.168169You can pass in a callable object instead of a string for the first item170in the tuple, and it will be used in place of the built in authentication171callbacks.172173Let's pretend that we have a web service that will only respond if the174``X-Pizza`` header is set to a password value. Unlikely, but just go with it.175176We simply need to define a callback function that will be used to update the177Request object, right before it is dispatched.178179::180181 def pizza_auth(r, username):182 """Attaches HTTP Pizza Authentication to the given Request object.183 """184 r.headers['X-Pizza'] = username185186 return r187188Then, we can make a request using our Pizza Auth::189190 >>> requests.get('http://pizzabin.org/admin', auth=(pizza_auth, 'kenneth'))191 <Response [200]>192193194Verbose Logging195---------------196197If you want to get a good look at what HTTP requests are being sent
8 unmodified lines
modified:requests/sessions.py+1 added-1 removed
110 unmodified lines
111 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.112 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.113 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.114 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.115 :param auth: (optional) AuthObject to enable Basic HTTP Auth.115 :param auth: (optional) Auth typle to enable Basic/Digest/Custom HTTP Auth.116 :param timeout: (optional) Float describing the timeout of the request.117 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.118 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.119 :param return_response: (optional) If False, an un-sent Request object will returned.
126 unmodified lines