Set a HTTP Cookie Response Header
·
246 words
·
2 minutes read
For many different reasons, there will be times when you need to keep data within a cookie to be sent with subsequent requests or read by the recipient. We can do this with Go’s standard library, or by using a package like gorilla’s session, but for this simple example we’ll use the standard library.
We’ve created a function called addCookie()
which takes a name, value and duration, and writes it directly against the ResponseWriter w
.
|
|
If we were to run this with go run cookie.go
and then use curl -I http:/localhost:8080
to check the response, we would see something like this:
< HTTP/1.1 200 OK
< Set-Cookie: TestCookieName=TestValue; Expires=Fri, 29 Sep 2017 19:18:25 GMT
< Date: Thu, 28 Sep 2017 19:18:25 GMT
< Content-Length: 12
< Content-Type: text/plain; charset=utf-8