[issue25570] urllib.request > Request.add_header("abcd", "efgh") fails with character ":" in first parameter string

Christian Rickert report at bugs.python.org
Fri Nov 6 15:39:12 EST 2015


New submission from Christian Rickert:

A piece of software stopped working after the update from Python 3.4.x to 3.5 (both Linux and Windows).

I found the cause in the  Request.add_header("abcd","efgh")  function:
If the first parameter string "abcd" contains a colon (":") character, a ValueError is raised and the execution stops.

This is an example code:

>>> import urllib.request
>>> import urllib.parse
>>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> data = data.encode('utf-8')
>>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
>>> # adding charset parameter to the Content-Type header.
>>> request.add_header("Content-Type:","application/x-www-form-urlencoded;charset=utf-8")
>>> with urllib.request.urlopen(request, data) as f:
...     print(f.read().decode('utf-8'))
...

This is the error:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\example.py", line 9, in <module>
    with urllib.request.urlopen(request, data) as f:
  File "C:\python\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\python\lib\urllib\request.py", line 465, in open
    response = self._open(req, data)
  File "C:\python\lib\urllib\request.py", line 483, in _open
    '_open', req)
  File "C:\python\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\python\lib\urllib\request.py", line 1268, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\python\lib\urllib\request.py", line 1240, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\python\lib\http\client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "C:\python\lib\http\client.py", line 1123, in _send_request
    self.putheader(hdr, value)
  File "C:\python\lib\http\client.py", line 1050, in putheader
    raise ValueError('Invalid header name %r' % (header,))
ValueError: Invalid header name b'Content-Type:'

Steps to reproduce:

Add a colon character anywhere into the first parameter of the  Request.add_header()  function and execute the code using Python 3.5.x.

----------
components: Library (Lib)
messages: 254214
nosy: crickert
priority: normal
severity: normal
status: open
title: urllib.request > Request.add_header("abcd","efgh") fails with character ":" in first parameter string
type: crash
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25570>
_______________________________________


More information about the Python-bugs-list mailing list