mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Some minor but helpful additions, like CookieOption. Relative: https://github.com/kataras/iris/issues/1018. Simple cookies example added too. Cookie encoding (side by side with the already session's cookie id encoding) and version upgrade will come tomorrow with a new HISTORY.md entry as well, stay tuned!
Former-commit-id: d14181fac998d32d77690b1b3e42b6c7c72f1ace
This commit is contained in:
64
_examples/cookies/basic/main.go
Normal file
64
_examples/cookies/basic/main.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
// Set A Cookie.
|
||||
app.Get("/cookies/{name}/{value}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
value := ctx.Params().Get("value")
|
||||
|
||||
ctx.SetCookieKV(name, value) // <--
|
||||
// Alternatively: ctx.SetCookie(&http.Cookie{...})
|
||||
//
|
||||
// If you want to set custom the path:
|
||||
// ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored"))
|
||||
//
|
||||
// If you want to be visible only to current request path:
|
||||
// (note that client should be responsible for that if server sent an empty cookie's path, all browsers are compatible)
|
||||
// ctx.SetCookieKV(name, value, iris.CookieCleanPath /* or iris.CookiePath("") */)
|
||||
// More:
|
||||
// iris.CookieExpires(time.Duration)
|
||||
// iris.CookieHTTPOnly(false)
|
||||
|
||||
ctx.Writef("cookie added: %s = %s", name, value)
|
||||
})
|
||||
|
||||
// Retrieve A Cookie.
|
||||
app.Get("/cookies/{name}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
|
||||
value := ctx.GetCookie(name) // <--
|
||||
// If you want more than the value then:
|
||||
// cookie, err := ctx.Request().Cookie(name)
|
||||
// if err != nil {
|
||||
// handle error.
|
||||
// }
|
||||
|
||||
ctx.WriteString(value)
|
||||
})
|
||||
|
||||
// Delete A Cookie.
|
||||
app.Delete("/cookies/{name}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
|
||||
ctx.RemoveCookie(name) // <--
|
||||
// If you want to set custom the path:
|
||||
// ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored"))
|
||||
|
||||
ctx.Writef("cookie %s removed", name)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// GET: http://localhost:8080/cookies/my_name/my_value
|
||||
// GET: http://localhost:8080/cookies/my_name
|
||||
// DELETE: http://localhost:8080/cookies/my_name
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
Reference in New Issue
Block a user