spyoungtech / grequests

Requests + Gevent = <3
https://pypi.python.org/pypi/grequests
BSD 2-Clause "Simplified" License
4.46k stars 331 forks source link

How to get body in exception_handler #134

Closed tedify closed 4 years ago

tedify commented 5 years ago

For example, I post some random data and I need to know which data was sent with error in exception_handler function, but:

def exception_handler(request, exception):
    print(request.body)

AttributeError: 'AsyncRequest' object has no attribute 'body'

spyoungtech commented 5 years ago

The body attribute is not directly accessible on AsyncRequest. However, all keywords are available in the kwargs attribute. So, you can perhaps extrapolate from that in your exception handler.

>>> req = grequests.get('https://httpbin.org/status/200', data={'foo':'bar'})
>>> req.kwargs
{'data': {'foo': 'bar'}}

On successful requests, you can access the traditional request object through the response attribute.

>>> req = grequests.get('https://httpbin.org/status/200', data={'foo':'bar'})
>>> req.send()
>>> req.response.request.body
'foo=bar'

Hope that helps you out.

Jack-Lewis1 commented 5 years ago

Why does it state "Note: You should probably use requests-threads or requests-futures instead." on the readme?

Any particular reason?

spyoungtech commented 5 years ago

@Jack-Lewis1 you can see Kenneth's (the previous owner) comments in #124 for more details, but now that this project is under new ownership, perhaps the readme deserves an update.

Though, this comment seems unrelated to this issue :)

spyoungtech commented 4 years ago

Closing this as there's not been activity on it in a while. Feel free to reopen if more assistance is needed.