user9684058
user9684058

Reputation:

How to convert the type *http Cookies into string using gin package

By ajax I'm setting the cookies and in go middleware I'm just taking the cookies but its a type of *http Cookies and I just want to generate the string then what should I have to use to do this.

Code:-

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," ")

Issue I want to generate the above output into a string after the =.can anyone tell me how I will do this.

I will tried this type of code

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

the above code will give me the error of :-

error

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

Can anyone help me. Thank you. if this is basic question then sorry I really don't know this.

Upvotes: 0

Views: 3471

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79546

You haven't specified which string you want, but assuming you want the cookie value, just access the Value field of the Cookie object:

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

Upvotes: 4

Related Questions