1
1
1programmer2019-08-19 10:31:33
go
1programmer, 2019-08-19 10:31:33

how to check cookie in golang?

I use the gin gonic library in Golang, set cookies like this:

func signHandler(c *gin.Context){
  ....
  http.SetCookie(c.Writer, &http.Cookie{
    Name: "session_token",
    Value: sessionToken.String(),
    Expires: time.Now().Add(120 * time.Second),
  })

  c.JSON(http.StatusCreated, gin.H{
            "message": credentials })
}

cookies are set. How to check for the existence of cookies?
//Это дает null
cookie, err := c.Request.Cookie("session_token")
//Так тоже не работает
cookie1, _ := c.Cookie("session_token")
  c.JSON(http.StatusUnauthorized, gin.H{
    "message": cookie,
    "err": err,
  })

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
1programmer, 2019-08-20
@1programmer

Solution:
Put it like this
We take it like this:
cookie, err := c.Cookie("session_token")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question