Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created October 29, 2016 18:25
Show Gist options
  • Save odeke-em/dac4751a5a7d6fe96f5bd59191f0d7a0 to your computer and use it in GitHub Desktop.
Save odeke-em/dac4751a5a7d6fe96f5bd59191f0d7a0 to your computer and use it in GitHub Desktop.
Inspecting bodies and requests from various methods
$ for method in "PATCH" "DELETE" "POST" "GET" "PUT" "AMETHOD";do go run main.go --body hello --method $method >> log;done && cat log
PATCH / HTTP/1.1
Host: 127.0.0.1:64022
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
DELETE / HTTP/1.1
Host: 127.0.0.1:64028
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
POST / HTTP/1.1
Host: 127.0.0.1:64034
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
GET / HTTP/1.1
Host: 127.0.0.1:64040
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
PUT / HTTP/1.1
Host: 127.0.0.1:64046
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
AMETHOD / HTTP/1.1
Host: 127.0.0.1:64052
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"strings"
)
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
w.Write(dump)
}))
defer ts.Close()
var body string
var method string
flag.StringVar(&body, "body", "", "body to send")
flag.StringVar(&method, "method", "POST", "method to use in client request")
flag.Parse()
req, err := http.NewRequest(method, ts.URL, strings.NewReader(body))
if err != nil {
log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
slurp, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", slurp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment