Created
January 4, 2017 07:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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