elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Case insensitive headers

Open pilcrowonpaper opened this issue 2 years ago • 5 comments
trafficstars

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.

pilcrowonpaper avatar Sep 04 '23 04:09 pilcrowonpaper

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()
})

lucsmachado avatar Sep 10 '23 19:09 lucsmachado

@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 avatar Nov 23 '23 21:11 binyamin

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

binyamin avatar Apr 07 '24 19:04 binyamin

+1 on this

itsjoeoui avatar Apr 07 '24 19:04 itsjoeoui

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:

karl-run avatar May 22 '24 16:05 karl-run