Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set-Cookie header doesn't work after changing the request url

Before:

<form action="/signup" method="POST">
server.post("/signup", () => {
    const sessionID = crypto.randomUUID()
    return {
        status: 302,
        headers: {
            "Location": "/",
            "Set-Cookie": `session=${sessionID}`
        }
    }
})

The cookie is successfully set in browser.

After changing the form route from /signup to /form/signup:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<form action="/form/signup" method="POST">
server.post("/form/signup", () => {
    const sessionID = crypto.randomUUID()
    return {
        status: 302,
        headers: {
            "Location": "/",
            "Set-Cookie": `session=${sessionID}`
        }
    }
})

When the request is handled cookie is now not set. In chrome dev tools there’s one entry in the cookie bar flashes for 0.5s and deletes itself. The flash also exists before, but the cookie eventually stayed. Why would changing route break the cookie behavior?

I’m using the http package in https://bun.sh/

>Solution :

You haven’t included a Path attribute in the cookie.

The first attempt is for the URL /signup so the default path is /.

The second attempt is for the URL /form/signup so the default path is /form/.

A cookie belonging to /form/ will not appear when you visit /.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading