Skip to content
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

Possibility to specify headers encoding #7943

Closed
1 task done
botsman opened this issue Dec 4, 2023 · 1 comment
Closed
1 task done

Possibility to specify headers encoding #7943

botsman opened this issue Dec 4, 2023 · 1 comment

Comments

@botsman
Copy link

botsman commented Dec 4, 2023

Is your feature request related to a problem?

I have a case where I need to send some non-ASCII characters through headers.

import aiohttp
import asyncio

async def make_request():
    headers = {
        "some": "Serviços",
    }
    async with aiohttp.ClientSession() as session:
        async with session.get(
            "https://postman-echo.com/get",
            headers=headers,
        ) as response:
            assert response.status == 200
            json_body = await response.json()
            assert json_body["headers"]["some"] == headers["some"]  #  AssertionError

asyncio.run(make_request())

But the sent header value does not match with the actual value: Serviços

This is due to the fact that aiohttp uses utf-8 for encoding headers.

Describe the solution you'd like

It looks like it will be enough to pass "encoding" parameter to the http_writer._serialize_headers function through either aiohttp.ClientSession or aiohttp.ClientRequest (or any other better way).

Describe alternatives you've considered

Currently I had to inherit StreamWriter and ClientRequest in my code and overwrite StreamWriter.write_headers function for it to use latin-1.

Related component

Client

Additional context

If that feature looks fine then I could probably take it and send a PR.

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@Dreamsorcerer
Copy link
Member

I have a case where I need to send some non-ASCII characters through headers.

This is not a valid HTTP message, and therefore we're unlikely to add anything that makes it easier for you to send invalid messages.

This is due to the fact that aiohttp uses utf-8 for encoding headers.

It should be percent encoded ASCII:
https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5-4

If it's not, then that's a bug. The spec for that percent encoding says:

Producers MUST use the "UTF-8" ([RFC3629]) character encoding.
https://www.rfc-editor.org/rfc/rfc8187.html#section-3.2.1

Maybe it's possible to use the raw, pre-encoded header value, rather than using aiohttp to encode it, but it's not something we're likely to look at.

@Dreamsorcerer Dreamsorcerer closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants