Last active
July 16, 2017 06:59
-
-
Save shawnsmithdev/cf5458b995c9507bc048f6ca2ba60727 to your computer and use it in GitHub Desktop.
CLI tool that prints the HTTP HEAD response for an url
This file contains 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" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
) | |
const defaultUrl = "https://example.com" | |
func main() { | |
url := defaultUrl | |
if len(os.Args) < 2 { | |
fmt.Println("Missing argument: url to head, using ", defaultUrl) | |
} else { | |
url = os.Args[1] | |
} | |
resp, err := http.Head(url) | |
if hasErr(err) { | |
return | |
} | |
defer resp.Body.Close() | |
dump, err := httputil.DumpResponse(resp, false) | |
if hasErr(err) { | |
return | |
} | |
fmt.Printf("%s", dump) | |
} | |
func hasErr(err error) bool { | |
erred := err != nil | |
if erred { | |
fmt.Println("Couldn't do it", err) | |
} | |
return erred | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment