Golang Read HTTP Header

The example below uses gorilla/mux package

package main

import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
for i,v := range r.Header {
fmt.Println(i, v)
}
fmt.Fprintf(w, r.Header.Get("User-Agent"))
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/", YourHandler)
log.Fatal(http.ListenAndServe(":8000", r))
}

With the codes above, try browsing it with this URL with your browser

http://localhost:8000

Your web server console will print all the HTTP headers, similar like the screenshot below.

Web browser will show the user agent.

< Back to all the stories I had written

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store