Skip to content

Instantly share code, notes, and snippets.

@178inaba
Created August 13, 2015 17:39
Show Gist options
  • Select an option

  • Save 178inaba/35f13a51fa99ae2dcc15 to your computer and use it in GitHub Desktop.

Select an option

Save 178inaba/35f13a51fa99ae2dcc15 to your computer and use it in GitHub Desktop.
print http header from go
package main
import (
"errors"
"flag"
"fmt"
"net/http"
)
func main() {
url := flag.String("url", "http://www.yahoo.co.jp/", "get header url. default is http://www.yahoo.co.jp/")
r := flag.Bool("r", false, "if it is true, do the redirect.")
flag.Parse()
c := new(http.Client)
if !*r {
c.CheckRedirect = func(req *http.Request, via []*http.Request) error { return errors.New("not redirect") }
}
resp, err := c.Get(*url)
if err != nil {
fmt.Println("error: ", err)
} else {
defer resp.Body.Close()
for k, v := range resp.Header {
fmt.Println("header key: ", k)
for _, value := range v {
fmt.Println("\theader value: ", value)
}
}
}
}
$ go run print_header.go
header key: Server
header value: nginx
header key: Date
header value: Thu, 13 Aug 2015 17:38:26 GMT
header key: Content-Type
header value: text/html; charset=UTF-8
header key: P3p
header value: policyref="http://privacy.yahoo.co.jp/w3c/p3p_jp.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
header key: Cache-Control
header value: private, no-cache, no-store, must-revalidate
header key: Vary
header value: Accept-Encoding
header key: Expires
header value: -1
header key: Pragma
header value: no-cache
header key: X-Xrds-Location
header value: https://open.login.yahooapis.jp/openid20/www.yahoo.co.jp/xrds
header key: X-Frame-Options
header value: SAMEORIGIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment