Skip to content
Myr

advanced docs

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 b730729 · Browse files · 1 file+57 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

1 file+57 added−1 removed

  • other:+57 added−1 removed

1 file+57 added−1 removed

modified:docs/user/advanced.rst+57 added-1 removed
43 unmodified lines
4445Event Hooks46-----------4748Requests has a hook system that allows you . This is useful for48Requests has a hook system that you can use to manipulate portions of49the request process, or signal event handling.5051Available hooks:5253``args``:54 A dictionary of the arguments being sent to Request().5556``pre_request``:57 The Request object, directly before being sent.5859``post_request``:60 The Request object, directly after being sent.6162``response``:63 The response generated from a Request.646566You can assign a hook function on a per-request basis by passing a67``{hook_name: callback_function}`` dictionary to the ``hooks`` request68paramaeter::6970 hooks=dict(args=print_url)7172That ``callback_function`` will receive a chunk of data as its first73argument.7475::7677 def print_url(args):78 print args['url']7980If an error occurs while executing your callback, a warning is given.8182If the callback function returns a value, it is assumed that it is to83replace the data that was passed in. If the function doesn't return84anything, nothing else is effected.8586Let's print some request method arguments at runtime::8788 >>> requests.get('http://httpbin', hooks=dict(args=print_url))89 http://httpbin90 <Response [200]>919293Verbose Logging94---------------9596If you want to get a good look at what HTTP requests are being sent97by your application, you can turn on verbose logging.9899To do so, just configure Requests with a stream to write to::100101 >>> requests.settings.verbose = sys.stderr102 >>> requests.get('http://httpbin.org/headers')103 2011-08-17T03:04:23.380175 GET http://httpbin.org/headers104 <Response [200]>