Skip to content

Instantly share code, notes, and snippets.

@tkrs
Created April 25, 2018 04:21
Show Gist options
  • Save tkrs/c3915bed655859f21b4f822169f9bc21 to your computer and use it in GitHub Desktop.
Save tkrs/c3915bed655859f21b4f822169f9bc21 to your computer and use it in GitHub Desktop.
Print the HTTP response in Go
package main
import (
"bytes"
"compress/gzip"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
// "debug/elf"
)
var (
dbg = flag.Bool("d", false, "debug mode")
og = flag.Bool("g", false, "out gzip mode")
url = flag.String("u", "", "specify a post endpoint")
fname = flag.String("f", "", "specify a post data")
)
func main() {
flag.Parse()
json, err := ioutil.ReadFile(*fname)
if err != nil {
log.Fatalln(err)
}
b, err := compress(json)
if err != nil {
log.Fatalln(err)
}
gz := b.Bytes()
r, err := gzip.NewReader(bytes.NewReader(gz))
if err != nil {
log.Fatalln(err)
}
if *og {
fmt.Println(string(gz))
}
resp, err := http.DefaultClient.Post(*url, "application/json", r)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if *dbg {
goDbg(resp)
}
if resp.StatusCode < 300 {
log.Println("StatusCode:", resp.StatusCode)
} else {
log.Fatalln("StatusCode:", resp.StatusCode)
}
}
func compress(s []byte) (b bytes.Buffer, err error) {
w := gzip.NewWriter(&b)
if _, err = w.Write(s); err != nil {
return
}
if err = w.Flush(); err != nil {
return
}
if err = w.Close(); err != nil {
return
}
return
}
func goDbg(resp *http.Response) {
hJoin := func(header http.Header) (ret string) {
for k, v := range header {
ret += fmt.Sprintf("%s = %+v</br>", k, v)
}
return
}
d := "# *Poston DEBUG MODE*\n\n"
d += "```" + `
__________ __
\______ \____ _______/ |_ ____ ____
| ___/ _ \/ ___/\ __\/ _ \ / \
| | ( <_> )___ \ | | ( <_> ) | \
|____| \____/____ > |__| \____/|___| /
\/ \/
` + "```\n"
d += "## HTTP\n\n"
d += "### Request\n\n"
d += "| Name | Value |\n"
d += "| ---- | ----- |\n"
d += fmt.Sprintf("| Method | %s |\n", resp.Request.Method)
d += fmt.Sprintf("| Host | %s |\n", resp.Request.Host)
d += fmt.Sprintf("| URL | %s |\n", resp.Request.URL)
d += fmt.Sprintf("| Proto | %s |\n", resp.Request.Proto)
d += fmt.Sprintf("| ProtoMajor | %d |\n", resp.Request.ProtoMajor)
d += fmt.Sprintf("| ProtoMinor | %d |\n", resp.Request.ProtoMinor)
d += fmt.Sprintf("| Header | %+v |\n", hJoin(resp.Request.Header))
d += fmt.Sprintf("| Content-Length | %d |\n", resp.Request.ContentLength)
d += fmt.Sprintf("| TransferEncoding | %+v |\n", resp.Request.TransferEncoding)
d += fmt.Sprintf("| Close | %v |\n", resp.Request.Close)
d += fmt.Sprintf("| Form | %+v |\n", resp.Request.Form)
d += fmt.Sprintf("| PostForm | %+v |\n", resp.Request.PostForm)
d += fmt.Sprintf("| MultipartForm | %+v |\n", resp.Request.MultipartForm)
d += fmt.Sprintf("| Trailer | %+v |\n", resp.Request.Trailer)
reqBody, _ := ioutil.ReadAll(resp.Request.Body)
d += fmt.Sprintf("| Body | `%s` |\n", string(reqBody))
d += "\n"
d += "### Response\n\n"
d += "| Name | Value |\n"
d += "| ---- | ----- |\n"
d += fmt.Sprintf("| Status | %s |\n", resp.Status)
d += fmt.Sprintf("| Proto | %s |\n", resp.Proto)
d += fmt.Sprintf("| ProtoMajor | %d |\n", resp.ProtoMajor)
d += fmt.Sprintf("| ProtoMinor | %d |\n", resp.ProtoMinor)
d += fmt.Sprintf("| Header | %+v |\n", hJoin(resp.Header))
d += fmt.Sprintf("| TransferEncoding | %+v |\n", resp.TransferEncoding)
d += fmt.Sprintf("| Trailer | %+v |\n", resp.Trailer)
respBody, _ := ioutil.ReadAll(resp.Body)
d += fmt.Sprintf("| Body | `%s` |\n", string(respBody))
fmt.Fprintln(os.Stderr, d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment