Skip to content

Commit 97865fa

Browse files
committed
Merge pull request #18 from agile6v/master
add support for request body (-body option)
2 parents d923ca3 + d7d5d94 commit 97865fa

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

httpdiff.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,15 @@ func vsi(a, b int, f string, v ...interface{}) bool {
5959
// do an HTTP request to a server and returns the response object and the
6060
// complete response body. There's no need to close the response body as this
6161
// will have been done.
62-
func do(method, host, ua, uri string) (*http.Response, []byte, error) {
63-
req, err := http.NewRequest(method, uri, nil)
62+
func do(method, req_body, host, ua, uri string) (*http.Response, []byte, error) {
63+
var err error
64+
var req *http.Request
65+
if strings.EqualFold("POST", method) || strings.EqualFold("PUT", method) {
66+
req, err = http.NewRequest(method, uri, strings.NewReader(req_body))
67+
} else {
68+
req, err = http.NewRequest(method, uri, nil)
69+
}
70+
6471
if err != nil {
6572
return nil, nil, err
6673
}
@@ -85,6 +92,7 @@ func do(method, host, ua, uri string) (*http.Response, []byte, error) {
8592

8693
func main() {
8794
method := flag.String("method", "GET", "Sets the HTTP method")
95+
req_body := flag.String("body", "", "Sets body data to send server")
8896
host := flag.String("host", "",
8997
"Sets the Host header sent with both requests")
9098
ignore := flag.String("ignore", "",
@@ -137,7 +145,7 @@ func main() {
137145
for i := 0; i < 2; i++ {
138146
wg.Add(1)
139147
go func(i int) {
140-
resp[i], body[i], err[i] = do(*method, *host, *ua, flag.Arg(i))
148+
resp[i], body[i], err[i] = do(*method, *req_body, *host, *ua, flag.Arg(i))
141149
wg.Done()
142150
}(i)
143151
}

0 commit comments

Comments
 (0)