Cookies
ctx.Response.Cookies provides access to all cookies that will be sent to the client when the HTTP response is sent.
Cookies allow you to keep information between different HTTP requests and are usually used to keep sessions, remember user preferences or store temporary identifiers.
This object allows you to manage the cookies that are pending from the response. To consult the cookies received from the client use Request Cookies.
Structure
ctx.Response.Cookies implement the following API:
Cookies
{
Get(key string) (Cookie, bool)
GetAll(key string) ([]Cookie, bool)
Set(cookie Cookie)
Append(cookie Cookie)
Delete(key string)
Has(key string) bool
IsEmpty() bool
Count() int
Keys() []string
Values() map[string][]Cookie
Entries() []Entry[Cookie]
} Methods
| Method | Description |
|---|---|
| Get() | Returns the first cookie associated with a name. |
| GetAll() | Returns all cookies associated with a name. |
| Set() | Set up a cookie. |
| Append() | Add a new cookie with the same name. |
| Delete() | Eliminate a cookie. |
| Has() | It indicates if a cookie exists. |
| IsEmpty() | It indicates whether the collection contains cookies. |
| Count() | Returns the number of cookies names available. |
| Keys() | Give back all the names in the collection. |
| Values() | Returns all cookies grouped by name. |
| Entries() | Returns all cookies as a collection of entries. |
Get()
Firma:func (Cookies) Get(key string) (Cookie, bool)
Get() returns the first cookie associated with a name and indicates whether the cookie exists.
func GET(ctx *gox.Context) gox.Page {
cookie, ok := ctx.Response.Cookies.Get("username")
if !ok {
return "Cookie not found"
}
return cookie.Value
} When the cookie does not exist, it returns an empty cookie and false.
When there are several cookies with the same name, Get() return only the first one.
Use Get() when you only need to access the first cookie associated with a name.
GetAll()
Firma:func (Cookies) GetAll(key string) ([]Cookie, bool)
GetAll() returns all cookies associated with a name and indicates whether the cookie exists.
func GET(ctx *gox.Context) gox.Page {
cookies, ok := ctx.Response.Cookies.GetAll("session")
if !ok {
return "Cookie not found"
}
return <pre>{{ cookies }}</pre>
} When there is no cookie with that name, return nil and false.
The returned slice is a separate copy. Modifying it does not change the cookies of the response.
Use GetAll() when you need to access all cookies associated with a name.
Set()
Firma:func (Cookies) Set(cookie Cookie)
Set() set up a cookie.
If there are other cookies with the same name, all are replaced by the new cookie.
func GET(ctx *gox.Context) gox.Page {
name := "George"
ctx.Response.Cookies.Set(gox.Cookie{
Name: "username",
Value: name,
})
return <h1>Hola {{ name }}</h1>
} Set() receives an object gox.Cookie that describes the cookie that will be sent to the browser. To know all its properties, see the object documentation gox.Cookie.
Remove a cookie from the browser
To indicate to the browser that a cookie should be removed, send an expired cookie.
func GET(ctx *gox.Context) gox.Page {
ctx.Response.Cookies.Set(gox.Cookie{
Name: "username",
Path: "/",
Expired: true,
})
return <span>Cookie eliminada desde el servidor</span>
} When Expired is true, GOX automatically generates an expired cookie for the browser to remove the stored cookie.
Append()
Firma:func (Cookies) Append(cookie Cookie)
Append() add a new cookie without removing existing cookies with the same name.
Although most applications use a single cookie per name, the HTTP standard allows you to send several cookies with the same name provided they differ, for example, in Path or Domain.
func GET(ctx *gox.Context) gox.Page {
ctx.Response.Cookies.Append(gox.Cookie{
Name: "session",
Value: "root",
Path: "/",
})
ctx.Response.Cookies.Append(gox.Cookie{
Name: "session",
Value: "admin",
Path: "/admin",
})
return <span>Se enviaron dos cookies con el mismo nombre</span>
} Use Append() when you need to send several cookies with the same name.
If you need to replace all existing cookies with that name, use Set().
Delete()
Firma:func (Cookies) Delete(key string)
Delete() removes all cookies associated with the specified name.
If the cookie does not exist, the operation has no effect.
func GET(ctx *gox.Context) gox.Page {
ctx.Response.Cookies.Set(gox.Cookie{
Name: "username",
Value: "George",
})
ctx.Response.Cookies.Delete("username")
return <span>No se envió la cookie "username"</span>
} After running Delete()The cookie is no longer part of the response.
Use Delete() when you need to remove an outstanding cookie before sending the response.
Has()
Firma:func (Cookies) Has(key string) bool
Has() indicates if a cookie exists.
func GET(ctx *gox.Context) gox.Page {
if ctx.Response.Cookies.Has("username") {
return "Cookie configured"
}
return "Cookie not configured"
} Use Has() when you only need to check if a cookie exists.
IsEmpty()
Firma:func (Cookies) IsEmpty() bool
IsEmpty() indicates whether the collection contains cookies.
func GET(ctx *gox.Context) gox.Page {
if ctx.Response.Cookies.IsEmpty() {
return "No cookies configured"
}
return "Cookies configured"
} Returns true when there is no cookie and false otherwise.
Count()
Firma:func (Cookies) Count() int
Count() returns the number of available cookies names.
func GET(ctx *gox.Context) gox.Page {
ctx.Response.Cookies.Set(gox.Cookie{Name: "username", Value: "George"})
ctx.Response.Cookies.Set(gox.Cookie{Name: "theme", Value: "dark"})
total := ctx.Response.Cookies.Count()
return total
} Each cookie name is counted once, regardless of the number of cookies associated.
Keys()
Firma:func (Cookies) Keys() []string
Keys() return all the names of the collection.
func GET(ctx *gox.Context) gox.Page {
keys := ctx.Response.Cookies.Keys()
return <pre>{{ keys }}</pre>
} The returned slice is a separate copy. Modifying it does not change the cookies of the response.
Values()
Firma:func (Cookies) Values() map[string][]Cookie
Values() returns all cookies grouped by name.
func GET(ctx *gox.Context) gox.Page {
values := ctx.Response.Cookies.Values()
return <pre>{{ values }}</pre>
} The returned map is a separate copy. Modifying it does not change the cookies of the response.
Entries()
Firma:func (Cookies) Entries() []Entry[Cookie]
Entries() returns all cookies as a collection of entries.
func GET(ctx *gox.Context) gox.Page {
entries := ctx.Response.Cookies.Entries()
for _, entry := range entries {
println(entry.Key)
for _, cookie := range entry.Value {
println(cookie.Value)
}
}
return nil
} The returned slice is a separate copy. Modifying it does not change the cookies of the response.
Use Entries() when you need to go through the whole collection of cookies.
Full example
func GET(ctx *gox.Context) gox.Page {
ctx.Response.Cookies.Set(gox.Cookie{
Name: "username",
Value: "George",
HttpOnly: true,
})
if ctx.Response.Cookies.Has("username") {
return "Hello George"
}
return "Cookie not found"
} Best practices
- Use
Set()when you only have to set a cookie by name. - Use
Append()when you need to add several cookies with the same name. - Use
HttpOnlyfor cookies containing sensitive information. - Active
Securewhen the application uses HTTPS. - Use
Expiredto indicate to the browser that a cookie should be removed. - Use
Delete()only to remove outstanding cookies from the response.
To learn more about ImportMap, check your documentation.