无法为golang的fasthttp框架设置cookie
Unable to set cookie for golang's fasthttp framework
我尝试使用 fasthttp 框架从服务器设置 cookie。但是响应中的 cookie 值设置不正确 header。我不知道我在下面的代码片段中错过了什么。
package main
import (
"log"
"github.com/valyala/fasthttp"
)
func main() {
if err := fasthttp.ListenAndServe(":8080", requestHandler); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/foo":
cook1 := fasthttp.Cookie{}
cook1.SetKey("cookie_key")
cook1.SetValue("cookie val")
cook1.SetMaxAge(3600000)
cook1.SetDomain("prabhakaran.com")
cook1.SetPath(("/"))
cook1.SetSecure(true)
ctx.Response.Header.Cookie(&cook1)
ctx.SetBody([]byte("this is completely new body contents"))
case "/bar":
//todo: Bar handler
// ctx.SetBody([]byte("111111111111111111111"))
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
我也试过ctx.Response.Header.SetCookie(&cook1)API。但它不会起作用。是否遗漏了任何内容?
ctx.Response.Header.Cookie(&cook1)
Cookie函数用于查看cookie值。所以,使用 SetCookie 函数代替 Cookie。如果你是运行http协议的服务器,去掉cook1.SetSecure(true)语句。
这里是 cookie-retrieve 的例子
// ParseTokenFromRequest is delegated to retrieved the token encoded in the request. The token can be sent in two different way.
// In first instance the method will try to find the token in the cookie. If the cookie is not provided in the cookie,
// then the research will continue analayzing the body of the request (URL ARGS,GET,POST).
// In case of token not found, an empty string will be returned
func ParseTokenFromRequest(ctx *fasthttp.RequestCtx) string {
token := string(ctx.Request.Header.Cookie("GoLog-Token")) // GoLog-Token is the hardcoded name of the cookie
log.Info("ParseTokenFromRequest | Checking if token is in the cookie ...")
if strings.Compare(token, "") == 0 { // No cookie provided :/ Checking in the request
log.Warn("ParseTokenFromRequest | Token is not in the cookie, retrieving from the request ...")
token = string(ctx.FormValue("token")) // Extracting the token from the request (ARGS,GET,POST)
if strings.Compare(token, "") == 0 { // No token provided in the request
log.Warn("ParseTokenFromRequest | Can not find the token! ...")
return "" // "COOKIE_NOT_PRESENT"
}
log.Info("ParseTokenFromRequest | Token found in request! ... | ", token)
} else {
log.Info("ParseTokenFromRequest | Token found in cookie! ... | ", token)
}
return token
}
这里是 cookie-set 的例子
//CreateCookie Method that return a cookie valorized as input (GoLog-Token as key)
func CreateCookie(key string, value string, expire int) *fasthttp.Cookie {
if strings.Compare(key, "") == 0 {
key = "GoLog-Token"
}
log.Debug("CreateCookie | Creating Cookie | Key: ", key, " | Val: ", value)
authCookie := fasthttp.Cookie{}
authCookie.SetKey(key)
authCookie.SetValue(value)
authCookie.SetMaxAge(expire)
authCookie.SetHTTPOnly(true)
authCookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
return &authCookie
}
authcookie := CreateCookie('','TEST_VALUE',120)
ctx.Response.Header.SetCookie(authcookie) // Set the token into the cookie headers
ctx.Response.Header.Set("GoLog-Token", token) // Set the token into a custom headers
这里是一个删除 cookie 的例子
ctx.Response.Header.DelCookie("GoLog-Token")
我尝试使用 fasthttp 框架从服务器设置 cookie。但是响应中的 cookie 值设置不正确 header。我不知道我在下面的代码片段中错过了什么。
package main
import (
"log"
"github.com/valyala/fasthttp"
)
func main() {
if err := fasthttp.ListenAndServe(":8080", requestHandler); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/foo":
cook1 := fasthttp.Cookie{}
cook1.SetKey("cookie_key")
cook1.SetValue("cookie val")
cook1.SetMaxAge(3600000)
cook1.SetDomain("prabhakaran.com")
cook1.SetPath(("/"))
cook1.SetSecure(true)
ctx.Response.Header.Cookie(&cook1)
ctx.SetBody([]byte("this is completely new body contents"))
case "/bar":
//todo: Bar handler
// ctx.SetBody([]byte("111111111111111111111"))
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
我也试过ctx.Response.Header.SetCookie(&cook1)API。但它不会起作用。是否遗漏了任何内容?
ctx.Response.Header.Cookie(&cook1)
Cookie函数用于查看cookie值。所以,使用 SetCookie 函数代替 Cookie。如果你是运行http协议的服务器,去掉cook1.SetSecure(true)语句。
这里是 cookie-retrieve 的例子
// ParseTokenFromRequest is delegated to retrieved the token encoded in the request. The token can be sent in two different way.
// In first instance the method will try to find the token in the cookie. If the cookie is not provided in the cookie,
// then the research will continue analayzing the body of the request (URL ARGS,GET,POST).
// In case of token not found, an empty string will be returned
func ParseTokenFromRequest(ctx *fasthttp.RequestCtx) string {
token := string(ctx.Request.Header.Cookie("GoLog-Token")) // GoLog-Token is the hardcoded name of the cookie
log.Info("ParseTokenFromRequest | Checking if token is in the cookie ...")
if strings.Compare(token, "") == 0 { // No cookie provided :/ Checking in the request
log.Warn("ParseTokenFromRequest | Token is not in the cookie, retrieving from the request ...")
token = string(ctx.FormValue("token")) // Extracting the token from the request (ARGS,GET,POST)
if strings.Compare(token, "") == 0 { // No token provided in the request
log.Warn("ParseTokenFromRequest | Can not find the token! ...")
return "" // "COOKIE_NOT_PRESENT"
}
log.Info("ParseTokenFromRequest | Token found in request! ... | ", token)
} else {
log.Info("ParseTokenFromRequest | Token found in cookie! ... | ", token)
}
return token
}
这里是 cookie-set 的例子
//CreateCookie Method that return a cookie valorized as input (GoLog-Token as key)
func CreateCookie(key string, value string, expire int) *fasthttp.Cookie {
if strings.Compare(key, "") == 0 {
key = "GoLog-Token"
}
log.Debug("CreateCookie | Creating Cookie | Key: ", key, " | Val: ", value)
authCookie := fasthttp.Cookie{}
authCookie.SetKey(key)
authCookie.SetValue(value)
authCookie.SetMaxAge(expire)
authCookie.SetHTTPOnly(true)
authCookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
return &authCookie
}
authcookie := CreateCookie('','TEST_VALUE',120)
ctx.Response.Header.SetCookie(authcookie) // Set the token into the cookie headers
ctx.Response.Header.Set("GoLog-Token", token) // Set the token into a custom headers
这里是一个删除 cookie 的例子
ctx.Response.Header.DelCookie("GoLog-Token")