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

Add to_dict method for request headers #1099

Open
antonpetrov145 opened this issue Dec 30, 2024 · 4 comments
Open

Add to_dict method for request headers #1099

antonpetrov145 opened this issue Dec 30, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@antonpetrov145
Copy link

Hello, first of all thank you for this marvelous framework, it is really enjoyable to work with especially as I'm coming from Flask.

I noticed that there are methods for returning dictionaries for query parameters, path parameters and form data are also dictionaries. Suggestion is because currently we are losing the ability to set a default value if the header is missing, if it is - we get always None, but what if we need to have some default value?

My idea is to have something like this

request.headers.to_dict()

which will return a dictionary with all present headers

{
    "content-type": "application/json",
    "X-Forwarded-To": "some IP",
    ...
}

Is this possible to achieve?

@antonpetrov145 antonpetrov145 added the enhancement New feature or request label Dec 30, 2024
@sansyrox
Copy link
Member

sansyrox commented Jan 1, 2025

Hey @antonpetrov145 👋

Thanks for opening an issue 😄 Yes, this can definitely be done. How does flask handle duplicate headers tho? i.e. the ones sharing the same name?

@antonpetrov145
Copy link
Author

@sansyrox Happy New Year, awesome that you answered 😄

In flask when there is a header that is doubled

curl http://api:5000/ \-H 'X-Custom: somecustomvalue' \-H 'X-Custom: another'

running

request.headers.get("X-Custom")

flask returns both values separated by a comma

'somecustomvalue,another'

@sansyrox
Copy link
Member

sansyrox commented Jan 4, 2025

Amazing, then @antonpetrov145 😄 Yes, it can be done then 😄

@antonpetrov145
Copy link
Author

Amazing, then @antonpetrov145 😄 Yes, it can be done then 😄

Thank you, this is awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants