Python Requests: Invalid Header Name

I am trying to send a request with the header: ": hello". However, the lead colon causes the script to function incorrectly and emit this trace:

Traceback (most recent call last):

(first few lines removed for my privacy)

File "C:\Python27\lib\site-packages\requests\api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 370, in send
    timeout=timeout
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 559, in urlopen
    body=body, headers=headers)
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 353, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Python27\lib\httplib.py", line 1057, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 1096, in _send_request
    self.putheader(hdr, value)
  File "C:\Python27\lib\httplib.py", line 1030, in putheader
    raise ValueError('Invalid header name %r' % (header,))
ValueError: Invalid header name ':hello'

Is there a workaround for this? My script:

import requests
headers = {'user-agent': 'alsotesting', ':hello': 'test'}
requests.post("my server", headers=headers)
+10
source share
2 answers

As stated in your error :header, the HTTP header name is not a valid name (you cannot start headers with ":" - see the documentation ). You have to change

headers = {'user-agent': 'alsotesting', ':hello': 'test'}

in

headers = {'user-agent': 'alsotesting', 'hello': 'test'}

: HTTP/2 , (. ). , , , Chrome Developer Tools, , Chrome -, SPDY, HTTP/2 ( SPDY/2), . , HTTP.

, , , HTTP, " Invalid header name

+6

== 2.18.4

.

import requests
requests.utils.check_header_validity = lambda x:return
0

Source: https://habr.com/ru/post/1620882/


All Articles