-
-
Notifications
You must be signed in to change notification settings - Fork 858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change default encoding to utf-8 in normalize_header_key
and normalize_header_value
functions
#3238
Comments
hey guys, can I work on this? |
|
Heya, thanks for the consideration... I think this may be valid, tho could you re-frame it so that you're describing the issue against public API. For example describe this using Edit: Okay, I see PR #3241 now. We don't want to support utf-8 in header keys which have a constrained set of allowed characters. I do think it's a sensible default for header values tho. |
In a request, if the headers contain non-ASCII characters and the encoding is not specified, an error will be raised. For example: from httpx import Headers
x = Headers(
{
"Referer": "https://www.google.com/search?q=テスト",
}
)
print(x) In the example code, the Referer field contains the Japanese word テスト, which will raise an error and interrupt the request. The same issue can occur in responses. If the server's response does not specify the encoding, httpx will use ASCII encoding to decode テスト, resulting in an error. Therefore, I suggest changing the default encoding from ASCII to UTF-8 to better handle such cases. |
@ZM25XC UTF-8 is not accepted in headers. You can only use ISO-8859-1 in HTTP headers according to rfc. If you want to put utf8 characters in the url and pass it to referer, you should url encode all utf8 characters first. In fact, this is how this url actually is. from httpx import Headers
from urllib.parse import quote
x = Headers(
{
"Referer": f"https://www.google.com/search?q={quote('テスト')}",
}
)
print(x)
# Headers({'referer': 'https://www.google.com/search?q=%E3%83%86%E3%82%B9%E3%83%88'}) |
Description
I have encountered decoding errors with some requests that use ASCII encoding. Changing the default encoding to UTF-8 resolves these errors. I propose updating the
normalize_header_key
andnormalize_header_value
functions in_utils.py
to use UTF-8 as the default encoding.Steps to Reproduce
normalize_header_key
ornormalize_header_value
with a non-ASCII string and no encoding specified.Example Code
Proposed Solution
Modify the _utils.py file to use UTF-8 as the default encoding:
Rationale
Using UTF-8 as the default encoding ensures that the functions can handle a wider range of input values without raising an error. UTF-8 encoding is capable of encoding a larger set of characters compared to ASCII.
The text was updated successfully, but these errors were encountered: