6

I see the ':' is error, but I can't find a way to solve it.

ValueError: Invalid header name b':authority'

It's the error:

File "tmall.py", line 23, in get_url
response = sessions.get(url=url,headers =headers)

File "E:\python\lib\site-packages\requests\sessions.py", line 501, in get
return self.request('GET', url, **kwargs)

File "E:\python\lib\site-packages\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)

File "E:\python\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)

File "E:\python\lib\site-packages\requests\adapters.py", line 423, in send
timeout=timeout

File "E:\python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)

File "E:\python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 356, in _make_request
conn.request(method, url, **httplib_request_kw)

File "E:\python\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)

File "E:\python\lib\http\client.py", line 1280, in _send_request
self.putheader(hdr, value)

File "E:\python\lib\http\client.py", line 1207, in putheader
raise ValueError('Invalid header name %r' % (header,))

It's the code:

import requests
headers = {
    ':authority':'list.tmall.com',
    ':method':'GET',
    ':path':path}
sessions = requests.session();
response = sessions.get(url=url,headers =headers)
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
user8608946
  • 79
  • 1
  • 1
  • 6

1 Answers1

1
import httplib

httplib._is_legal_header_name = re.compile(r':|\A[^:\s][^:\r\n]*\Z').match
Clock Slave
  • 7,627
  • 15
  • 68
  • 109
1mtrue
  • 60
  • 4
  • 13
    How does this answer the question? Please edit your answer and elaborate on that. – planetmaker Nov 22 '18 at 10:06
  • 2
    Rewrite the regular expression of httplib. _is_legal_header_name sorry, – 1mtrue Nov 26 '18 at 01:30
  • 3
    For Python 3.7, it's `import http.client` `http.client._is_legal_header_name = re.compile(rb'[^\s][^:\r\n]*').fullmatch` – Jing He Nov 26 '19 at 16:45
  • 1
    i get `Invalid header value b'token ghp\n'` when calling `Github(token).get_user()`. I have `lf` as `line ending` of the token file. What is `b` at the begin? – Timo Jun 16 '21 at 15:51