Friday, 15 January 2021

Should I pass the host header when I make an HTTPS connection in Python?

I am getting an HTTP 400 - Bad Request error while making an XML-RPC over HTTPS in Python 3.8.

It seems like this issue is happening when we are supplying Host in the HTTPS request header, without skip_host=True in the putrequest (doc) call before that. Are both these info's -- skip_host argument and Host header -- mutually exclusive? If so, which one should I use?

import http.client

connection = http.client.HTTPSConnection("duckduckgo.com", "443")

connection.putrequest("GET", "/")  # needs skip_host=True if Host has to be supplied
connection.putheader("User-Agent", "Python/3.8")
connection.putheader("Host", "duckduckgo.com")  # needs skip_host=True to work
connection.endheaders()

response = connection.getresponse()
print(response.status, response.reason)


from Should I pass the host header when I make an HTTPS connection in Python?

No comments:

Post a Comment