gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:03:09 26 4
gpt4 key购买 nike

通过 ajax,我正在设置 cookie,在 go 中间件中,我只是获取 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 ,只需访问 Value field of the Cookie object :

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

关于go - 如何使用 gin 包将类型 *http Cookies 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50291431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com