kennethreitz/requests — pull request #1338 by Lukasa (2013-04-30 19:56:33+00:00)
Title: Header keys should be native strings.

_Note_: This PR follows a discussion on IRC. The logs can be found [here](https://botbot.me/freenode/python-requests/msg/2942059/).

### Summary
This fixes a problem that @https://github.com/va1en0k _would_ have had in #1335 if the current Case-Insensitive Dict didn't behave so weirdly (see #1333 for progress on that).

Right now, Requests unconditionally encodes all header values as bytes on all Python versions. Kenneth and I are (tentatively) in agreement that header values should actually be native strings on all platforms. This pull request introduces code that explicitly encodes or decodes string objects to satisfy that requirement.

### Notes
Firstly, **this is a breaking API change on Python 3**. On Python 2 the behaviour will be the same unless the user has changed `sys.defaultencoding`, and if they have they deserve what's coming to them. On Python 3, all header keys on `PreparedRequest`s will go from being bytes to unicode objects, and so anyone who has correctly programmed to this interface will have to change their code.

Secondly, this code currently uses ASCII as the codec in both directions. I think this is OK for now. I'm pretty sure that header values should actually be Latin-1, but I can understand if we want to keep the current behaviour in place. I'm open to feedback here.