Headers of the response in the form of an email.message.EmailMessage instance.
(That is a bit surprising. Why email? An example how to access the headers would be nice.)
typeshed annotates it as a HTTPMessage. (Is that the same as EmailMessage?)
This issue explains HTTPMessage should not be considered a Mapping. (Not sure if that is relevant.)
From the EmailMessage docs:
The following methods implement the mapping-like interface for accessing the message’s headers. Note that there are some semantic differences between these methods and a normal mapping (i.e. dictionary) interface. For example, in a dictionary there are no duplicate keys, but here there may be
duplicate message headers.
So I guess the code is at least not ideal, as it will miss duplicate keys, and I should use items() to get all headers?
import http.client
response: http.client.HTTPResponse = ...
for name, value in response.headers.items():
print(name, value)
for name in response.headers:
Header Content-Length 0
Header MyHeader Hello
Header MyHeader Hello
for name, value in response.headers.items():
Header Content-Length 0
Header MyHeader Hello
Header MyHeader World
(The “Hello” is repeated and the “World” is missing.)
Oh, I overlooked that iterating response.headers gives only header names! The documentation says that you should use the get_all() method to get all the values for a given header name.
I find this Mapping-like behaviour of an object with multiple possible keys confusing.