Fiber: 馃悰 Set cookie then get its value right back

Created on 13 Sep 2020  路  4Comments  路  Source: gofiber/fiber

Fiber version

v1.14.4

Issue description

I'm having problem setting cookie then get its value right back. Somehow Set cookie is not working and the cookie value remains as before.

Code snippet

package main

import (
    "time"

    "github.com/gofiber/fiber"
)

const cookieName = "test_cookie"

func main() {
    app := fiber.New()

    // http://localhost:3000/q?p=something
    app.Get("/q", func(c *fiber.Ctx) {
        cookie := new(fiber.Cookie)
        cookie.Expires = time.Now().Add(24 * time.Hour)
        tests := c.Cookies(cookieName)
        tests += ", " + c.Query("p")

        // Set cookie
        cookie.Name = cookieName
        cookie.Value = tests
        c.Cookie(cookie)

        // Get cookie value right back
        testn := c.Cookies(cookieName)
        c.Send(tests + " => " + testn)
    })

    app.Listen(3000)
}

Repeatably make request to http://localhost:3000/q?p=AAA will get

, AAA, AAA, AAA, AAA => , AAA, AAA, AAA

Anticipated behavior: cookie value get back is the same as what has just set before.
Actual behavior: cookie value got back remain the same:

Please double-check.
thx

鈽笍 Bug

Most helpful comment

Hi, I think this is because you are not actually sending the cookie back to the client before you check for it. Cookies are set in the response header, and retrieved by looking at the request header.

You set the cookie with c.Cookie, which adds the Set-Cookie header to the response object. However, since you did not send the response yet (by returning from the handler) the client has no idea about the cookie and c.Cookies will return "". Does that make sense?

Here is a working example:

package main

import (
    "time"

    "github.com/gofiber/fiber"
)

const cookieName = "test_cookie"

func main() {
    app := fiber.New()

        // set cookie
    // http://localhost:3000/q?p=something
    app.Get("/q", func(c *fiber.Ctx) {
        cookie := new(fiber.Cookie)
        cookie.Expires = time.Now().Add(24 * time.Hour)

        // Set cookie
        cookie.Name = cookieName
        cookie.Value = c.Query("p")
        c.Cookie(cookie)

        c.SendString("Cookie set") // since we return here, the `Set-Cookie` header is sent
    })

        // get cookie (this should be ran after the above)
    app.Get("/c", func(c *fiber.Ctx) {
        c.SendString(c.Cookies(cookieName)) 
    })

    app.Listen(3000)
}

All 4 comments

Is it related to zero allocation? I presume that setting & getting cookies uses browsers' own cookies space.

Hi, I think this is because you are not actually sending the cookie back to the client before you check for it. Cookies are set in the response header, and retrieved by looking at the request header.

You set the cookie with c.Cookie, which adds the Set-Cookie header to the response object. However, since you did not send the response yet (by returning from the handler) the client has no idea about the cookie and c.Cookies will return "". Does that make sense?

Here is a working example:

package main

import (
    "time"

    "github.com/gofiber/fiber"
)

const cookieName = "test_cookie"

func main() {
    app := fiber.New()

        // set cookie
    // http://localhost:3000/q?p=something
    app.Get("/q", func(c *fiber.Ctx) {
        cookie := new(fiber.Cookie)
        cookie.Expires = time.Now().Add(24 * time.Hour)

        // Set cookie
        cookie.Name = cookieName
        cookie.Value = c.Query("p")
        c.Cookie(cookie)

        c.SendString("Cookie set") // since we return here, the `Set-Cookie` header is sent
    })

        // get cookie (this should be ran after the above)
    app.Get("/c", func(c *fiber.Ctx) {
        c.SendString(c.Cookies(cookieName)) 
    })

    app.Listen(3000)
}

Oh, thanks for the fast response @hi019, all good now!

Great, I'll close this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bashery picture bashery  路  4Comments

Aguezz picture Aguezz  路  4Comments

mewben picture mewben  路  3Comments

Terisback picture Terisback  路  3Comments

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments