Handling Cookies with Gin Framework in Go

Gopal Agrawal
2 min readJun 8, 2023

--

Cookies are an essential component of modern web development, and they play a crucial role in maintaining state across HTTP requests. In this article, we’ll explore how to handle cookies in Go using the Gin framework.

Gin is a popular Go web framework that provides a fast and efficient way to build web applications. It includes a middleware engine that allows you to modify the request and response objects. This makes it easy to add cookie handling functionality to your Gin application.

Setting a Cookie in Gin

Setting a cookie in Gin is straightforward. You can use the SetCookie method provided by the gin.Context object to set a cookie on the client's browser.

  • name (string): The name of the cookie to be set.
  • value (string): The value of the cookie.
  • maxAge (int): The maximum age of the cookie in seconds. If set to 0, the cookie will be deleted immediately. If set to a negative value, the cookie will be a session cookie and will be deleted when the browser is closed.
  • path (string): The URL path for which the cookie is valid. Defaults to "/", meaning the cookie is valid for all URLs.
  • domain (string): The domain for which the cookie is valid. Defaults to the current domain.
  • secure (bool): If set to true, the cookie will only be sent over secure (HTTPS) connections.
  • httpOnly (bool): If set to true, the cookie will be inaccessible to JavaScript and can only be sent over HTTP(S) connections.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

Here’s an example:

func setCookieHandler(c *gin.Context) {
c.SetCookie("user", "John Doe", 3600, "/", "localhost", false, true)
c.String(http.StatusOK, "Cookie has been set")
}

In this example, we are setting a cookie with the name “user” and the value “John Doe”. The cookie will expire in 3600 seconds (1 hour), and it will be accessible from all paths on the localhost domain. The last two parameters specify that the cookie should not be accessible from JavaScript and should be sent over HTTPS only.

Reading a Cookie in Gin

Reading a cookie in Gin is just as easy as setting one. You can use the gin.Context object's Request method to read a cookie from the client's browser. Here’s an example:

func getCookieHandler(c *gin.Context) {
cookie, err := c.Cookie("user")
if err != nil {
c.String(http.StatusNotFound, "Cookie not found")
return
}
c.String(http.StatusOK, "Cookie value: %s", cookie)
}

In this example, we are getting the value of the “user” cookie. If the cookie does not exist, we return a 404 error. If the cookie exists, we return its value using the String method of the gin.Context.

Deleting a Cookie in Gin

To delete a cookie in Gin, we can use the SetCookie method with an expiration time in the past. Here is an example:

func deleteCookieHandler(c *gin.Context) {
c.SetCookie("user", "", -1, "/", "localhost", false, true)
c.String(http.StatusOK, "Cookie has been deleted")
}

In this example, we are deleting the “user” cookie by setting its value to an empty string and its expiration time to a negative value. This will cause the browser to delete the cookie immediately.

--

--

Gopal Agrawal

🚀 Golang enthusiast, more interested in SaaS! Freelancer building many innovative platforms from scratch. Let's code the next big thing together! 👩‍💻✨