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