jcayzac
jcayzac12mo ago

std/http/setCookie doesn't work?

It seems to only support single cookies:
import * as cookies from "https://deno.land/std@0.211.0/http/cookie.ts"
var h = new Headers()

cookies.setCookie(h, {name: 'foo', value: 'FOO'})
// Headers { "set-cookie": "foo=FOO" }
// OK (works as expected)

cookies.setCookie(h, {name: 'bar', value: 'BAR'})
// Headers { "set-cookie": "bar=BAR" }
// Not OK: Cookie `foo` was deleted for some reason!

cookies.setCookie(h, [{name: 'x', value: 'X'}, {name: 'y', value: 'Y'}])
// Headers { "set-cookie": "bar=BAR" }
// Not OK: None of the cookies were set
import * as cookies from "https://deno.land/std@0.211.0/http/cookie.ts"
var h = new Headers()

cookies.setCookie(h, {name: 'foo', value: 'FOO'})
// Headers { "set-cookie": "foo=FOO" }
// OK (works as expected)

cookies.setCookie(h, {name: 'bar', value: 'BAR'})
// Headers { "set-cookie": "bar=BAR" }
// Not OK: Cookie `foo` was deleted for some reason!

cookies.setCookie(h, [{name: 'x', value: 'X'}, {name: 'y', value: 'Y'}])
// Headers { "set-cookie": "bar=BAR" }
// Not OK: None of the cookies were set
Also, it assumes the Headers object is a container for response headers. For request headers it the right header would be cookie:, not set-cookie:.
4 Replies
razzi
razzi12mo ago
That is indeed weird behavior, however you can pass multiple cookies using the Headers constructor: https://deno.land/std@0.211.0/http/cookie.ts?s=getSetCookies#Examples
const headers = new Headers([
["Set-Cookie", "lulu=meow; Secure; Max-Age=3600"],
["Set-Cookie", "booya=kasha; HttpOnly; Path=/"],
]);
const headers = new Headers([
["Set-Cookie", "lulu=meow; Secure; Max-Age=3600"],
["Set-Cookie", "booya=kasha; HttpOnly; Path=/"],
]);
(as a workaround)
jcayzac
jcayzacOP12mo ago
Right, but then I don't need std/http. The lack of a cookie jar for fetch() is pretty sad. I would put that pretty high on my wishlist…
marvinh.
marvinh.12mo ago
I'd consider this a bug. Can you file an issue for that on the std repo? cc @iuioiua
jcayzac
jcayzacOP12mo ago
Ah, the bug is in how Deno.inspect() reports the content of the Headers object, not in the std/http library!
No description