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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"io"
	"net/http"
	"time"
)

func main() {
	http.HandleFunc("/", indexHandler)
	http.ListenAndServe(":8080", nil)
}

func indexHandler(w http.ResponseWriter, req *http.Request) {
	// Create a cookie which lasts 30 mins
	addCookie(w, "TestCookieName", "TestValue", 30*time.Minute)
	// Write our example
	io.WriteString(w, "Hello world!")
}

// addCookie will apply a new cookie to the response of a http request
// with the key/value specified.
func addCookie(w http.ResponseWriter, name, value string, ttl time.Duration) {
	expire := time.Now().Add(ttl)
	cookie := http.Cookie{
		Name:    name,
		Value:   value,
		Expires: expire,
	}
	http.SetCookie(w, &cookie)
}

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

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!