Cookies
ctx.Request.Cookies provides access to all cookies sent by the client during the current request.
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 only allows to consult the cookies received from the client. To manage the cookies that will be sent to the client use Response Cookies.
func GET(ctx *gox.Context) gox.Page {
sessionID, ok := ctx.Request.Cookies.Get("session")
if !ok {
return <p>Guest</p>
}
return <p>{{ sessionID }}</p>
} Structure
ctx.Request.Cookies implement the following API:
Cookies
{
Get(key string) (string, bool)
GetAll(key string) []string
Has(key string) bool
IsEmpty() bool
Count() int
Keys() []string
Values() []string
Entries() map[string][]string
} All methods are read only.
The names of cookies distinguish between capital letters and lower letters.
Methods
| Method | Description |
|---|---|
| Get() | Returns the first value associated with a cookie. |
| GetAll() | Returns all values associated with a cookie. |
| Has() | It indicates if a cookie exists. |
| IsEmpty() | It indicates whether the collection contains cookies. |
| Count() | Returns the number of cookies available. |
| Keys() | Returns the names of all cookies. |
| Values() | Returns the first value associated with each cookie. |
| Entries() | Returns all cookies along with their values. |
Get()
Firma:func (Cookies) Get(key string) (string, bool)
Get() returns the first value associated with a cookie.
func GET(ctx *gox.Context) gox.Page {
sessionID, ok := ctx.Request.Cookies.Get("session")
// ok == true
// sessionID == "5db4dcb0..."
return <p>{{ sessionID }}</p>
} When the cookie does not exist, the second returned value will be false.
Use Get() when you only need to read a specific cookie.
GetAll()
Firma:func (Cookies) GetAll(key string) []string
GetAll() returns all the values associated with a cookie.
func GET(ctx *gox.Context) gox.Page {
values := ctx.Request.Cookies.GetAll("preferences")
return <ul>
<:for value range={{ values }}>
<li>{{ value }}</li>
</:for>
</ul>
} Although most cookies contain a single value, this method allows access to all values associated with the same name.
Has()
Firma:func (Cookies) Has(key string) bool
Has() indicates if a cookie exists.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Cookies.Has("session") {
return <p>Authenticated user</p>
}
return <p>Guest</p>
} Has() just check the existence of the cookie.
IsEmpty()
Firma:func (Cookies) IsEmpty() bool
IsEmpty() indicates whether the collection does not contain cookies.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Cookies.IsEmpty() {
return <p>No cookies received.</p>
}
return <p>Cookies available.</p>
} Most web applications receive at least one cookie when the user has already login or previously interacted with the site.
Count()
Firma:func (Cookies) Count() int
Count() returns the number of cookies available.
func GET(ctx *gox.Context) gox.Page {
total := ctx.Request.Cookies.Count()
return <p>{{ total }}</p>
} Each cookie name counts as a single element within the collection.
Keys()
Firma:func (Cookies) Keys() []string
Keys() return the names of all cookies.
func GET(ctx *gox.Context) gox.Page {
keys := ctx.Request.Cookies.Keys()
return <ul>
<:for key range={{ keys }}>
<li>{{ key }}</li>
</:for>
</ul>
} It can be used to inspect cookies sent by the client.
Values()
Firma:func (Cookies) Values() []string
Values() returns the first value associated with each cookie.
func GET(ctx *gox.Context) gox.Page {
values := ctx.Request.Cookies.Values()
return <ul>
<:for value range={{ values }}>
<li>{{ value }}</li>
</:for>
</ul>
} If you need to keep all the values associated with each cookie, use Entries().
Entries()
Firma:func (Cookies) Entries() map[string][]string
Entries() returns all cookies along with all their values.
func GET(ctx *gox.Context) gox.Page {
cookies := ctx.Request.Cookies.Entries()
// cookies["session"]
// cookies["language"]
// cookies["theme"]
return <p>{{ len(cookies) }}</p>
} Each map key represents the name of a cookie and its associated value contains all the values received for that cookie.
Full example
The following example shows several common operations on cookies in an request.
func GET(ctx *gox.Context) gox.Page {
cookies := ctx.Request.Cookies
sessionID, _ := cookies.Get("session")
language, _ := cookies.Get("language")
keys := cookies.Keys()
return <main>
<p>Session: {{ sessionID }}</p>
<p>Language: {{ language }}</p>
<p>Total cookies: {{ cookies.Count() }}</p>
<h2>Cookies recibidas</h2>
<ul>
<:for key range={{ keys }}>
<li>{{ key }}</li>
</:for>
</ul>
</main>
} Best practices
- Use
Get()to read a specific cookie. - Check the boolean value returned by
Get()when the cookie may not exist. - Use
Has()when you only need to check if a cookie is present. - Use
Entries()when you need to go through the whole collection. - It avoids assuming that a cookie always exists.
- You don't store sensitive information directly on cookies sent by the client.
- Always value the content of cookies before using it.
To learn more about RequestSee your documentation. To learn more about HeadersSee your documentation. To learn more about QuerySee your documentation. To learn more about FormSee your documentation. To learn more about SessionSee your documentation. To learn more about Context, check your documentation.