Improve this page

Cookies

An HTTP cookie is a small piece of data that a server sends to the user's web brower. The browser can store this data and send it back to the same server, even after the browser restart (unlike a browser session).

(HTTP) cookies are commonly used to save users state (like whether the user logged-in). See https://golang.org/pkg/net/http/#Cookie for more information on cookies in Go.

Setting a Cookie

func MyHandler(c buffalo.Context) error {
  // ...
  c.Cookies().Set("user_id", user.ID, 30 * 24 * time.Hour)
  // ...
}

Setting a Cookie with Expiration

func MyHandler(c buffalo.Context) error {
  // ...
  exp := time.Now().Add(365 * 24 * time.Hour) // expire in 1 year
  c.Cookies().SetWithExpirationTime("user_id", user.ID, exp)
  // ...
}

Getting a Cookie

func MyHandler(c buffalo.Context) error {
  value, err := c.Cookies().Get("user_id")
  if err != nil {
    return err
  }
  return c.Render(200, r.String(value))
}

Deleting a Cookie

func MyHandler(c buffalo.Context) error {
  c.Cookies().Delete("user_id")
  // ...
}