Skip to content

Instantly share code, notes, and snippets.

@shapeshed
Created November 14, 2016 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shapeshed/89b087805e9035a6c5ea67c53361238c to your computer and use it in GitHub Desktop.
Save shapeshed/89b087805e9035a6c5ea67c53361238c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
)
func main() {
debug := os.Getenv("DEBUG")
client := &http.Client{}
request, err := http.NewRequest("GET", "https://ifconfig.co", nil)
if err != nil {
log.Fatal(err)
}
if debug == "1" {
debugRequest, err := httputil.DumpRequestOut(request, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", debugRequest)
}
response, err := client.Do(request)
defer response.Body.Close()
if debug == "1" {
debugResponse, err := httputil.DumpResponse(response, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", debugResponse)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", body)
}
@shapeshed
Copy link
Author

shapeshed commented Nov 14, 2016

DEBUG=1 go run httputil.go

GET / HTTP/1.1
Host: ifconfig.co
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip

HTTP/1.1 200 OK
Content-Length: 13
Connection: keep-alive
Content-Type: text/plain; charset=utf-8
Date: Mon, 14 Nov 2016 15:07:01 GMT
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload

68.235.53.83
68.235.53.83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment