mercredi 18 janvier 2017

Python: request.has_header(header) always returns false

Using the urllib in python 3.6, I have created a request and applied headers to it. However, Python appears to completely ignore that directive and when I run

req.has_header('Content-Type')

the result is false. This goes for other headers as well, except for 'Connection'. Interestingly, 'connection' returns false as well. I have not been able to find a good resource for how precisely to declare headers in Python that consistently work (and Python is apparently being extremely case-sensitive about header names), so if anyone has any recommendations I'd be interested to hear them!

My relevant source is:

head = {
    'content-type': 'application/x-www-form-urlencoded',
    'connection': 'keep-alive',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}

req = Request(url, post_bytes, head)
print(req.has_header('content-type')

Substitute 'content-type' for any of the other given headers and the result is still false. Its almost like the default python request includes no headers but...surely that wouldn't be the case?

Thanks for any insight!




Aucun commentaire:

Enregistrer un commentaire