如何使用 gin 包将类型 *http Cookie 转换为字符串



通过 ajax 我正在设置 cookie,在中间件中我只是拿 cookie,但它是一种 *http Cookie,我只想生成字符串,然后我应该使用什么来执行此操作。

代码:-

headerToken,_ := c.Request.Cookie("X-Test-Header")
fmt.Println(headerToken)
output is `X-Test-Header=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InB1bmVldEBiay5jb20iLCJwYXNzd29yZCI6IjEyMzQ0In0.x0INnR3anZXjPEtwZSmG3pAX5RZjJSmZ`
//but now I'm splits this and converting into the string 
headerTok := strings.Join(headerToken," ")

问题我想在=之后将上述输出生成为字符串,任何人都可以告诉我我将如何做到这一点。

我会尝试这种类型的代码

s := strings.Split(headerToken, "=")
ip, port := s[0], s[1]
fmt.Println(ip, port)

上面的代码会给我错误:-

错误

cannot use headerToken (type *http.Cookie) as type string in argument to strings.Split

谁能帮我。谢谢。如果这是基本问题,那么对不起,我真的不知道这一点。

您尚未指定所需的字符串,但假设您想要 cookie,只需访问Cookie对象的Value字段:

cookie, err := req.Cookie("cookie-name")
if err != nil {
panic(err.Error())
}
value := cookie.Value

最新更新