-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
net/http, net/textproto: add a Header method to get multiple values []string, with canonicalized key #34799
Comments
Both the http.Header and textproto.MIMEHeader types specify that "[t]o access multiple values of a key, or to use non-canonical keys, access the map directly." Also, given how simply this function can be written yourself, why is it necessary to include it in the standard library?
|
Thanks @smasher164 for the explanation. When using I thought it would be useful to provide such method so developers don't have to worry about case sensitivity when providing header key to get multiple values just like getting a single value. I'm cool with having a utility function to perform the same. Hope no other developers would hit the similar confusion as I did. |
I'd prefer to change the representation of net/http (and probably net/textproto) Header to not be a map and instead be a struct containing an internal map (along with other stuff), and enforce that we only store canonicalized keys. That's part of #23707. That said, a new method for now sounds fine. |
I'm in favor of calling it |
I like |
Change https://golang.org/cl/200760 mentions this issue: |
Change https://golang.org/cl/217129 mentions this issue: |
…Values These methods are new in Go 1.14. Updates #34799 Updates #36878 Change-Id: I063f5cb4638d7e0716e6ce2a8c3fffced51bbd34 Reviewed-on: https://go-review.googlesource.com/c/go/+/217129 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
In order to get multiple values from a header, we have to use a standard map expression. However, we need to be aware that keys are canonicalized (
header["My-Key"]
) otherwiseheader["my-key"]
will not return any value. Or need to usetextproto.CanonicalMIMEHeaderKey()
explicitly.header.Get(key)
allows to get the first value and internally usestextproto.CanonicalMIMEHeaderKey
to canonicalize the key before getting the value.Is there a reason not to provide, something like
header.GetValues(key)
, which returns[]string
?The text was updated successfully, but these errors were encountered: