Cookies

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")
  // ...
}

Copyright © 2017 Buffalo. All rights reserved.

Buffalo Gopher designed by Ashley McNamara — Original Gopher designed by Renee French.