-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
filter duplicate header #80
Comments
Hi @roccomuso, thanks for reporting the issue 👍 !
In your example, you are manually appending a lowercase
The behaviour when triggering a fetch with a So to me, the bug comes from the browser implementation of But anyway, despite that I'll consider checking if a I will not be able to work on that in the following days as I don't have a lot of free time, but I'll keep the issue open and update the status when I'll be available to work on |
Thanks for the immediate reply! But yeah, it worked fine in node.js but got issues in chrome runtime. Better handle the case |
I stumbled again in this issue 1 year later and completely forgot I had encounter this already. |
I just stumbled across this issue too, although from the angle of specifying the character set as part of the content-type header. The API I'm hitting requires us to specify I love wretch and appreciate you may not have time to address this yourself. I'll take a look at raising a PR for this. |
For anybody who needs this, I've written a small middleware that deduplicates headers with the same name. import { Headers, HeadersInit } from 'node-fetch'
import { ConfiguredMiddleware } from 'wretch'
export const manipulateHeaders =
(callback: (headers: Headers) => Headers): ConfiguredMiddleware =>
(next) =>
(url, { headers, ...opts }) => {
const nextHeaders = callback(
new Headers(headers as HeadersInit),
) as unknown as globalThis.Headers
return next(url, { ...opts, headers: nextHeaders })
}
export const dedupeHeaders = (
dedupeHeaderLogic: Record<
string,
(values: string[]) => Iterable<string>
> = {},
) => {
const deduperMap = new Map(
Object.entries(dedupeHeaderLogic).map(([k, v]) => [k.toLowerCase(), v]),
)
const dedupe = (key: string) =>
deduperMap.get(key.toLowerCase()) ?? ((values: string[]) => new Set(values))
return manipulateHeaders((headers) => {
Object.entries(headers.raw()).forEach(([key, values]) => {
const deduped = Array.from(dedupe(key)(values))
headers.delete(key)
deduped.forEach((value, index) =>
headers[index ? 'append' : 'set'](key.toLowerCase(), value),
)
})
return headers
})
} By default, it will deduplicate identical values for a given header. This can be used as follows: wretch().middlewares([dedupeHeaders()]) If there is a specific header for which the defaults cause problems, then you can provide a callback to handle deduplication yourself: wretch().middlewares([dedupeHeaders({
Accept: (values) => values.filter(v => v !== '*/*')
})]) I hope this is helpful to somebody. |
Since @jimmed provided a great workaround in the form of a middleware (thanks! 🙇) I think this issue can be safely closed. |
Since you have a lot of possibilities when gluing together methods you can easily mess up the requests.
I did something like:
and saw that in my requests I had duplicate header:
Content-Type: application/json, application/json
. Causing my server to reject the request.This shouldn't be possible. Maybe worth filtering duplicate header?
The text was updated successfully, but these errors were encountered: