requests: Requests 2.11: check_header_validity failed on header with integer value
Hi,
Since requests 2.11, all my calls using requests for my app are broken. After debugging, it seems that this version does not accept header with integer value, like it was before.
2.10:
In [1]: import requests
In [2]: requests.get('http://bing.com', headers={'Content-Length': 42})
Out[2]: <Response [200]>
2.11
In [1]: import requests
In [2]: requests.get('http://bing.com', headers={'Content-Length': 42})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\utils.py in check_header_validity(header)
751 try:
--> 752 if not pat.match(value):
753 raise InvalidHeader("Invalid return character or leading space in header: %s" % name)
TypeError: expected string or bytes-like object
During handling of the above exception, another exception occurred:
InvalidHeader Traceback (most recent call last)
<ipython-input-2-ae7ec2933e34> in <module>()
----> 1 requests.get('http://bing.com', headers={'Content-Length': 42})
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\api.py in get(url, params, **kwargs)
68
69 kwargs.setdefault('allow_redirects', True)
---> 70 return request('get', url, params=params, **kwargs)
71
72
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\api.py in request(method, url, **kwargs)
54 # cases, and look like a memory leak in others.
55 with sessions.Session() as session:
---> 56 return session.request(method=method, url=url, **kwargs)
57
58
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
455 hooks = hooks,
456 )
--> 457 prep = self.prepare_request(req)
458
459 proxies = proxies or {}
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\sessions.py in prepare_request(self, request)
388 auth=merge_setting(auth, self.auth),
389 cookies=merged_cookies,
--> 390 hooks=merge_hooks(request.hooks, self.hooks),
391 )
392 return p
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
293 self.prepare_method(method)
294 self.prepare_url(url, params)
--> 295 self.prepare_headers(headers)
296 self.prepare_cookies(cookies)
297 self.prepare_body(data, files, json)
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\models.py in prepare_headers(self, headers)
407 for header in headers.items():
408 # Raise exception on invalid header value.
--> 409 check_header_validity(header)
410 name, value = header
411 self.headers[to_native_string(name)] = value
D:\VSProjects\azure-sdk-for-python\env3.5\Lib\site-packages\requests\utils.py in check_header_validity(header)
754 except TypeError:
755 raise InvalidHeader("Header value %s must be of type str or bytes, "
--> 756 "not %s" % (value, type(value)))
757
758
InvalidHeader: Header value 42 must be of type str or bytes, not <class 'int'>
We define ‘Content-Length’ in each request. Anyway, using an integer for an header which is semantically an integer makes sense no?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (15 by maintainers)
Commits related to this issue
- requests only supports strings in headers See https://github.com/kennethreitz/requests/issues/3477 — committed to Tigge/platinumshrimp by reggna 8 years ago
- fix headers to comply with requests>=2.11 as of 2.11, requests raises an exception if header values are not `str` or `bytes`. see: https://github.com/kennethreitz/requests/issues/3477 — committed to little-dude/bambou by little-dude 8 years ago
- Fix SKIP_BETAMAX header to work with newer versions of requests. See https://github.com/kennethreitz/requests/issues/3477 for more info. TLDR; Headers can only be basestring/bytes. SKIP_BETAMAX is a... — committed to 13steinj/praw by 13steinj 8 years ago
- Fix SKIP_BETAMAX header to work with newer versions of requests. See https://github.com/kennethreitz/requests/issues/3477 for more info. TLDR; Headers can only be basestring/bytes. SKIP_BETAMAX is a... — committed to 13steinj/praw by 13steinj 8 years ago
- fix headers to comply with requests>=2.11 as of 2.11, requests raises an exception if header values are not `str` or `bytes`. see: https://github.com/kennethreitz/requests/issues/3477 — committed to little-dude/bambou by little-dude 8 years ago
- Header values need to be strings This has seemingly always been documented, and broken in a recent release. Reference: https://github.com/kennethreitz/requests/issues/3477 Signed-off-by: Patrick Ui... — committed to mikem23/koji-playground by puiterwijk 8 years ago
- Stringify the Content-Length header Recent versions of requests no longer accept integer header values. See https://github.com/kennethreitz/requests/issues/3477 Signed-off-by: Wieland Hoffmann <the... — committed to mineo/python-musicbrainz-ngs by mineo 8 years ago
- Stop using integer header values. See https://github.com/kennethreitz/requests/issues/3477. — committed to carsonyl/councillor-party by carsonyl 8 years ago
- requests header value must be str: https://github.com/kennethreitz/requests/issues/3477 — committed to kevinsteves/pan-python by kevinsteves 7 years ago
- Fixes Content-Length vaule bug with latest Requests. See also: https://github.com/kennethreitz/requests/issues/3477 Signed-off-by: Kohei Maeda <mkouhei@palmtb.net> — committed to mkouhei/swiftsc by mkouhei 7 years ago
- Stringify the Content-Length header Recent versions of requests no longer accept integer header values. See https://github.com/kennethreitz/requests/issues/3477 Signed-off-by: Wieland Hoffmann <the... — committed to mineo/python-musicbrainz-ngs by mineo 8 years ago
- Add test for target parameters and fix requests lib error 1. Adds test for target-* parameters. 2. Fixes an error that is caused by requests not accepting non-string header parameters https://gith... — committed to openstack/python-mistralclient by deleted user 7 years ago
- Convert object_size to string before adding it to a header. Since python-requests 2.11 (see https://github.com/requests/requests/issues/3477), integer values in headers are no longer automatically co... — committed to panlinux/libcloud by panlinux 6 years ago
- Convert object_size to string before adding it to a header. Since python-requests 2.11 (see https://github.com/requests/requests/issues/3477), integer values in headers are no longer automatically co... — committed to apache/libcloud by panlinux 6 years ago
- Stringify the Content-Length header Recent versions of requests no longer accept integer header values. See https://github.com/kennethreitz/requests/issues/3477 Signed-off-by: Wieland Hoffmann <the... — committed to mineo/python-musicbrainz-ngs by mineo 8 years ago
Seems like a breaking change for a dot-release, no?
@lmazuel done ✨🍰✨
@lmazuel p.s. thank you for caring 😛