elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.53k stars 226 forks source link

Case insensitive headers #99

Open pilcrowonpaper opened 1 year ago

pilcrowonpaper commented 1 year ago

Currently, Set-Cookie and set-cookie are treated as different headers when using Context.set.headers even thought headers are case insensitive (RFC 2616).

context.set.headers["Set-Cookie"] = cookie1;
context.set.headers["set-cookie"] = cookie2;

To keep the current API, set.headers will need to use setters and getters, so I'd argue implementing context.response solution mentioned in #98 makes more sense here.

lucsmachado commented 1 year ago

Encountered a similar issue while trying to validate headers using a schema.

headers: t.Object({
    "User-Agent": t.String()
})

It triggers a validation error:

$ curl localhost:3000

Invalid headers, 'User-Agent': Expected string

Expected: {
  "User-Agent": ""
}

Found: {
  "host": "localhost:3000",
  "user-agent": "curl/8.0.1",
  "accept": "*/*"
}

Simply changing it to lowercase fixes the problem:

headers: t.Object({
    "user-agent": t.String()
})
binyamin commented 11 months ago

@SaltyAom Perhaps we should use the Fetch API's Headers interface for this? It handles case-sensitivity automatically. Besides, it's a Web standard which Deno, Bun and Node.js 18 all support.

binyamin commented 7 months ago

BTW, one of the reasons I don't use elysia is because it's hard to control the HTTP Headers.

itsjoeoui commented 7 months ago

+1 on this

karl-run commented 5 months ago

I just got stuck on this as well, especially since typing it with the swagger plugin like this:

image

works wonders with the swagger UI:

image

But then you get 422 errors when you're testing the APIs :sweat_smile: