golang-web-dev icon indicating copy to clipboard operation
golang-web-dev copied to clipboard

Use existing cookie to authenticate an API request

Open sammit20 opened this issue 7 years ago • 1 comments

Is there any way to authenticate using existing cookie and gets details from the API request? I am very new to golang, Also went through the Chapter 29 but couldn't relate to the case I am working on. If you have reading on that can you please point it out?

My Case: I want to connect to the REST API of remote server via golang. However the authentication to the api is via cookie only. So need to pass the existing cookie information to get authenticated and pull out the details.

sammit20 avatar Jul 09 '18 09:07 sammit20

req, err := http.NewRequest("GET", "/signout", nil) if err != nil { t.Fatal(err.Error()) } res := httptest.NewRecorder()

handler := http.HandlerFunc(signout)
c := &http.Cookie{
	Name:   "session",
	Value:  cookieToSet,
	MaxAge: cAge,
}
req.AddCookie(c)
handler.ServeHTTP(res, req)

tonyjafar avatar Mar 20 '19 11:03 tonyjafar