requests / toolbelt

A toolbelt of useful classes and functions to be used with python-requests
https://toolbelt.readthedocs.org
Other
995 stars 183 forks source link

BodyPart headers are bytes not str #168

Open martinxsliu opened 7 years ago

martinxsliu commented 7 years ago

The BodyPart.headers dictionary returned by the MultipartDecoder uses bytes for keys and values, while the requests.Response object uses string keys and values. Is there a reason that the headers should be bytes? Otherwise, I propose that we make the two consistent, then we can use the same logic to process normal and multipart responses.

I'll be happy to submit a PR for this, it looks like a small change.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/39912866-bodypart-headers-are-bytes-not-str?utm_campaign=plugin&utm_content=tracker%2F418367&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F418367&utm_medium=issues&utm_source=github).
sigmavirus24 commented 7 years ago

The headers are not actually fully decoded which is exactly why they're bytes. We don't do any post processing (i.e., what would be required to handle RFC 2231 headers).

martinxsliu commented 7 years ago

I'm not particularly familiar with the state of world regarding non-ascii headers.

I do see that we are decoding the headers here using the encoding provided to MultipartDecoder. Is the reasoning for that to simply pass it through to the header parser without regard to the underlying meaning of the string?

sigmavirus24 commented 7 years ago

Is the reasoning for that to simply pass it through to the header parser without regard to the underlying meaning of the string?

Headers have a specific format. Header values have specific semantic meaning. Parsing the header format to make them easier to use doesn't really equate to us needing to decode headers. There's no equivalence there.