psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.03k stars 9.3k forks source link

Validation of header type makes it imposible to use a str subclass #6321

Closed startmat closed 1 year ago

startmat commented 1 year ago

Hi, the validation of header type makes it impossible to use a str subclass.

code of validation: https://github.com/psf/requests/blob/ec553c275fe915ab8c179a530907b564892d2922/requests/utils.py#L1036

I am rendering a the header by using the Django Template engine which returns a django.utils.safestring.SafeString. When using this as a header I get the

requests.exceptions.InvalidHeader: Header part ('application/xml') from {'accept': 'application/xml'} must be of type str or bytes, not <class 'django.utils.safestring.SafeString'> exception.

May be using a construct with isinstance(ret, tuple(HEADER_VALIDATORS)): could solve the problem.

thx & br Matthias

sigmavirus24 commented 1 year ago

In the future, please search closed and open issues before creating new ones that are duplicates.