Cookie/AddCookie

Cookie方法从HTTP请求头中获取cookie,AddCookie则添加cookie至HTTP响应头。

Example

  1. package main
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "strconv"
  6. "github.com/vicanso/elton"
  7. "github.com/vicanso/elton/middleware"
  8. )
  9. func main() {
  10. e := elton.New()
  11. e.Use(middleware.NewDefaultResponder())
  12. e.GET("/", func(c *elton.Context) (err error) {
  13. cookie, _ := c.Cookie("jt")
  14. if cookie == nil {
  15. _ = c.AddCookie(&http.Cookie{
  16. Name: "jt",
  17. Value: strconv.Itoa(rand.Int()),
  18. })
  19. }
  20. c.Body = cookie
  21. return
  22. })
  23. err := e.ListenAndServe(":3000")
  24. if err != nil {
  25. panic(err)
  26. }
  27. }