Note that there are some explanatory texts on larger screens.

plurals
  1. POPython requests hook returns a value to cause exception
    primarykey
    data
    text
    <p>The <a href="http://docs.python-requests.org/en/latest/user/advanced.html#event-hooks" rel="nofollow">Documentation</a> for python requests module says for hooks that "If the callback function returns a value, it is assumed that it is to replace the data that was passed in. If the function doesn’t return anything, nothing else is effected"</p> <p>Now i am trying to return a value(int in my case) from my hook function and it throws an exception. This will be valid in all the cases when the return value is an object that DOESNOT have the raw() method defined for it.</p> <p>Here is some code</p> <pre><code>def hook(resp,**kwargs): print resp.url return 1 def main() s = requests.Session() s.hooks = {"response":hook} r = s.get("http://localhost/index.html") </code></pre> <p>And here is the exception:</p> <pre><code>http://localhost/index.html Traceback (most recent call last): File "/home/talha/ws/test.py", line 85, in &lt;module&gt; main() File "/home/talha/ws/test.py", line 72, in main r = s.get("http://localhost/index.html") File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 347, in get return self.request('GET', url, **kwargs) File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 335, in request resp = self.send(prep, **send_kwargs) File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 446, in send extract_cookies_to_jar(self.cookies, request, r.raw) AttributeError: 'int' object has no attribute 'raw' </code></pre> <p>The code in sessions.py @line 446 is trying to extract cookies after the dispatch_hook..From source</p> <pre><code> # Response manipulation hooks r = dispatch_hook('response', hooks, r, **kwargs) # Persist cookies extract_cookies_to_jar(self.cookies, request, r.raw) </code></pre> <p>Either the documentation needs to change or the handling needs to be re-worked. What is the best way to handle this ?</p> <p>[update]</p> <p>Based on the comments I tried to return the base <code>response</code> object. Turns out it cannot be used in that manner also since some of its fields are initialized to <code>None</code>.</p> <p>Newer code:</p> <pre><code>def hook(resp, **kwargs): obj = requests.Response() return obj </code></pre> <p>Exception thrown now:</p> <pre><code>Traceback (most recent call last): File "/home/talha/ws/test.py", line 88, in &lt;module&gt; main() File "/home/talha/ws/test.py", line 75, in main r = s.get("http://localhost/index.html") File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 347, in get return self.request('GET', url, **kwargs) File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 335, in request resp = self.send(prep, **send_kwargs) File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 446, in send extract_cookies_to_jar(self.cookies, request, r.raw) File "/usr/lib/python2.7/site-packages/requests/cookies.py", line 108, in extract_cookies_to_jar res = MockResponse(response._original_response.msg) AttributeError: 'NoneType' object has no attribute '_original_response' </code></pre> <p>What seems is that i will have to implement a full pseudo response?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload