With the Python httplib "HTTPConnection" object, how do you send a header without a value?

StackOverflow https://stackoverflow.com/questions/15165706

  •  16-03-2022
  •  | 
  •  

I have the following code but httplib still treats the header 'emptyheader' as key:value pair.

h = httplib.HTTPConnection("somewhere:5000")
headers = {}
headers['emptyheader'] = None
h.request('POST', '/somewhere', '', headers)

How do I send the sane request but with a valueless header 'emptyheader'?

有帮助吗?

解决方案 2

No way to do it using httplib. 'headers' parameter must be a dictionary. httplib then constructs headers as colon-separated name-value pairs.

And I think requests module does the same thing.

So, as an option, try curl:

subprocess.call(['curl', '-i', '-H', '"emptyheader"', '"http://somewhere:5000/somewhere"'])

But, I'm not sure why do you need it: http headers are key:value pairs by design.

其他提示

Set the header value to '':

headers['emptyheader'] = ''

would output:

emptyheader: \r\n

which is conform with the HTTP specification for message headers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top