2016-10-20 20:30:53 +02:00
|
|
|
+++
|
|
|
|
title = "Cookies"
|
|
|
|
description = "Handling cookie in Echo"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
2016-10-20 20:30:53 +02:00
|
|
|
name = "Cookies"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 6
|
|
|
|
+++
|
|
|
|
|
|
|
|
Cookie is a small piece of data sent from a website and stored in the user's web
|
|
|
|
browser while the user is browsing. Every time the user loads the website, the browser
|
|
|
|
sends the cookie back to the server to notify the user's previous activity.
|
|
|
|
Cookies were designed to be a reliable mechanism for websites to remember stateful
|
|
|
|
information (such as items added in the shopping cart in an online store) or to
|
|
|
|
record the user's browsing activity (including clicking particular buttons, logging
|
|
|
|
in, or recording which pages were visited in the past). Cookies can also store
|
|
|
|
passwords and form content a user has previously entered, such as a credit card
|
|
|
|
number or an address.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Cookie Attributes
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Attribute | Optional
|
|
|
|
:--- | :---
|
|
|
|
`Name` | No
|
|
|
|
`Value` | No
|
|
|
|
`Path` | Yes
|
|
|
|
`Domain` | Yes
|
|
|
|
`Expires` | Yes
|
|
|
|
`Secure` | Yes
|
|
|
|
`HTTPOnly` | Yes
|
|
|
|
|
2016-10-26 01:56:12 +02:00
|
|
|
Echo uses go standard `http.Cookie` object to add/retrieve cookies from the context received in the handler function.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Create a Cookie
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
func writeCookie(c echo.Context) error {
|
2016-10-26 01:56:12 +02:00
|
|
|
cookie := new(http.Cookie)
|
|
|
|
cookie.Name = "username"
|
|
|
|
cookie.Value = "jon"
|
|
|
|
cookie.Expires = time.Now().Add(24 * time.Hour)
|
2016-10-20 20:30:53 +02:00
|
|
|
c.SetCookie(cookie)
|
|
|
|
return c.String(http.StatusOK, "write a cookie")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-10-26 01:56:12 +02:00
|
|
|
- Cookie is created using `new(http.Cookie)`.
|
|
|
|
- Attributes for the cookie are set assigning to the `http.Cookie` instance public attributes.
|
2016-10-20 20:30:53 +02:00
|
|
|
- Finally `c.SetCookie(cookies)` adds a `Set-Cookie` header in HTTP response.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Read a Cookie
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
func readCookie(c echo.Context) error {
|
|
|
|
cookie, err := c.Cookie("username")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-26 01:56:12 +02:00
|
|
|
fmt.Println(cookie.Name)
|
|
|
|
fmt.Println(cookie.Value)
|
2016-10-20 20:30:53 +02:00
|
|
|
return c.String(http.StatusOK, "read a cookie")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- Cookie is read by name using `c.Cookie("username")` from the HTTP request.
|
|
|
|
- Cookie attributes are accessed using `Getter` function.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Read all Cookies
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
func readAllCookies(c echo.Context) error {
|
|
|
|
for _, cookie := range c.Cookies() {
|
2016-10-26 01:56:12 +02:00
|
|
|
fmt.Println(cookie.Name)
|
|
|
|
fmt.Println(cookie.Value)
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
return c.String(http.StatusOK, "read all cookie")
|
|
|
|
}
|
|
|
|
```
|