-
Notifications
You must be signed in to change notification settings - Fork 204
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 helper function for headers #147
Comments
If you have not yet started working on this feature, I can submit a PR. How? By the way, I'm imagining composables like below so that H3 methods can be aligned: // Request phase
const headers = useHeaders(event) // all keys are lower-cased as `req.getHeaders()`
const value = useHeader(event, name) // `name` is case-insensitive as `req.getHeader(name)`
headers[name.toLowerCase()] === value // true
const body = useBody(event)
// Response phase
appendHeader(event, name, value)
send(event, data, type)
sendStream(event, data) We might want to make |
It seems that Lines 75 to 76 in 56bfe0a
So, |
Sure thing @NozomuIkuta. I think we can introduce |
Thanks @NozomuIkuta! These helper methods are good to have. Strictly speaking, my proposal/idea went a bit further and included a structured access to the headers, i.e. parse them: // Instead of
getHeader(event, "Content-Type")
// returns string: text/html; charset=UTF-8
// One has
getHeader(event, "Content-Type")
// returns object: {mediaTypes: ["text/html"], charsets: ["UTF-8"]} Don't you think it makes sense and be helpful for devs to have this structured access? |
As far as I can tell useBody is no longer available (see unjs/h3#147 (comment) && https://nuxt.com/docs/guide/directory-structure/server#handling-requests-with-body).
Since its quite common to read the header strings, it would be nice if h3 could provide a helper function for this, which parses the headers and provides a structured way to access various common headers. For example, inspired by https://hapi.dev/module/accept/api/?v=5.0.2#parseallheaders one could have
The text was updated successfully, but these errors were encountered: