Effect CommunityEC
Effect Community•2y ago•
66 replies
Stephen Bluck

Troubleshooting Multiple Cookie Setting in HTTP Responses

Hello again! Is there a way to use the platform Http to append headers. I found a bug in my code when I realised not all cookies where being set in a single response:
// Example 1
// Does not create multiple cookies - boo :(
const response = Http.response
  .json({ foo: "bar" })
  .pipe(
    Effect.map(Http.response.setHeader("Set-Cookie", "one=1")),
    Effect.map(Http.response.setHeader("Set-Cookie", "two=2")),
    Effect.map(Http.response.setHeader("Set-Cookie", "three=3")),
    Effect.map(Http.response.setHeader("Set-Cookie", "four=4")),
    Effect.map(Http.response.toWeb),
    Effect.runSync,
  );

console.log(response.headers.get("Set-Cookie"));
// four=4

// Example 2
// Creates multiple cookies - yey!
const headers = new Headers();

headers.append("Set-Cookie", "one=1");
headers.append("Set-Cookie", "two=2");
headers.append("Set-Cookie", "three=3");
headers.append("Set-Cookie", "four=4");

const response2 = new Response("http://localhost", { headers });

console.log(response2.headers.get("Set-Cookie"));
// one=1; two=2; three=3; four=4;
Was this page helpful?