From 083b91bfaaa37848744557353f676ddc83cb4d71 Mon Sep 17 00:00:00 2001 From: loveuer <loveuer@live.com> Date: Mon, 29 Jan 2024 19:16:36 +0800 Subject: [PATCH] feat: add ctx.cookies func --- ctx.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ctx.go b/ctx.go index 1f0b443..498ca68 100644 --- a/ctx.go +++ b/ctx.go @@ -62,6 +62,23 @@ func (c *Ctx) Path(overWrite ...string) string { return path } +func (c *Ctx) Cookies(key string, defaultValue ...string) string { + var ( + dv = "" + ) + + if len(defaultValue) > 0 { + dv = defaultValue[0] + } + + cookie, err := c.Request.Cookie(key) + if err != nil || cookie.Value == "" { + return dv + } + + return cookie.Value +} + func (c *Ctx) Next() error { c.index++ s := len(c.handlers)