spyoungtech / grequests

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

Passing non-string header values causes request to silently fail #106

Closed edam closed 6 years ago

edam commented 7 years ago

Passing headers that contain non-string values, e.g.:

headers = {
    'X-MeaningOfLife': 42, # not a string
}

Causes the request to fail and, although response is None, no exception is thrown and there is no other sign that the request was not made (that I'm aware of).

spyoungtech commented 6 years ago

Exceptions that occur with AsyncRequests are caught and stored in an exception attribute.

>>> headers = {'foo': 42}
>>> req = grequests.get('https://httpbin.org', headers=headers)
>>> req.send()
<grequests.AsyncRequest object at 0x000001E23EC567F0>
>>> req.response
>>> req.exception # None
InvalidHeader("Value for header {foo: 42} must be of type str or bytes, not <class 'int'>",)

If you're using grequests.map to send the requests, you should use the exception_handler argument to deal with errors.

>>> def exception_handler(req, ex):
...     print(ex)
...     return locals()
...
>>> grequests.map([grequests.get('https://httpbin.org', headers=headers)], exception_handler=exception_handler)
Value for header {foo: 42} must be of type str or bytes, not <class 'int'>
[{'ex': InvalidHeader("Value for header {foo: 42} must be of type str or bytes, not <class 'int'>",), 'req': <grequests.AsyncRequest object at 0x000001E23F99A470>}]

Hope that helps.