Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Created January 4, 2017 07:01
Show Gist options
  • Save gnilchee/63cd0dd17d5835a0a95a8c036976301c to your computer and use it in GitHub Desktop.
Save gnilchee/63cd0dd17d5835a0a95a8c036976301c to your computer and use it in GitHub Desktop.
Get content and status code of headers and body and print to screen. Url is pulled from commandline arguments
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
// Verify correct number of commandline arugments
if len(os.Args) != 2 {
log.Fatalf("Usage: %s URL", os.Args[0])
}
// Grabbing url from commandline argument
url := os.Args[1]
// Headers
fmt.Println("Header\n")
res_header, err := http.Head(url)
if err != nil {
log.Fatal(err)
}
fmt.Println("Header Status Code:", res_header.StatusCode)
for k, v := range res_header.Header {
fmt.Printf("%s: %s\n", k, v)
}
// Body
fmt.Println("\nBody\n")
res_body, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
fmt.Println("Body Status Code:", res_body.StatusCode)
body_out, err := ioutil.ReadAll(res_body.Body)
res_body.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", body_out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment