Skip to content

Commit 9236cac

Browse files
committed
add -H and -f options
1 parent 0f21086 commit 9236cac

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

main.go

+59-2
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,68 @@ import (
88
"log"
99
"net/http"
1010
"net/url"
11+
"os"
1112
"strings"
1213
)
1314

15+
type httpHeader struct {
16+
key string
17+
value string
18+
}
19+
20+
type httpHeaderSlice []httpHeader
21+
22+
func (s *httpHeaderSlice) String() string {
23+
ret := ""
24+
for i, kv := range *s {
25+
ret = ret + kv.key + ":" + kv.value
26+
if i < len(*s)-1 {
27+
ret = ret + ","
28+
}
29+
}
30+
return ret
31+
}
32+
33+
func (s *httpHeaderSlice) Set(value string) error {
34+
parts := strings.SplitN(value, ":", 2)
35+
if len(parts) != 2 {
36+
return fmt.Errorf("invalid header should be in `key:value` format")
37+
}
38+
if len(parts[0]) == 0 {
39+
return fmt.Errorf("invalid header, key is empty")
40+
}
41+
*s = append(*s, httpHeader{
42+
key: parts[0],
43+
value: parts[1],
44+
})
45+
return nil
46+
}
47+
1448
func main() {
1549
var targetURL string
1650
var body string
51+
var bodyFile string
1752
var contentType string
1853
var accessKey string
1954
var secretKey string
2055
var method string
2156
var verbose bool
57+
var headers httpHeaderSlice
2258
flag.StringVar(&method, "X", "GET", "http request method")
59+
flag.Var(&headers, "H", "HTTP headers")
2360
flag.StringVar(&targetURL, "u", "", "URL")
24-
flag.StringVar(&body, "d", "", "request body")
61+
flag.StringVar(&body, "d", "", "request body, cannot be used with -f")
62+
flag.StringVar(&bodyFile, "f", "", "filename to load body from, cannot be used with -d")
2563
flag.StringVar(&contentType, "t", "application/json", "content type")
2664
flag.StringVar(&accessKey, "ak", "", "QINIU access key")
2765
flag.StringVar(&secretKey, "sk", "", "QINIU secret key")
2866
flag.BoolVar(&verbose, "v", false, "show verbose (HTTP headers)")
2967
flag.Parse()
3068

69+
if body != "" && bodyFile != "" {
70+
log.Fatalf("cannot specify both body content and body file")
71+
}
72+
3173
client := &http.Client{}
3274
client.Transport = &qiniuMacTransport{
3375
AccessKey: accessKey,
@@ -43,7 +85,17 @@ func main() {
4385
if reqURL.Scheme == "" {
4486
reqURL.Scheme = "http"
4587
}
46-
req, err := http.NewRequest(method, reqURL.String(), bytes.NewReader([]byte(body)))
88+
var req *http.Request
89+
if body != "" || bodyFile == "" {
90+
req, err = http.NewRequest(method, reqURL.String(), bytes.NewReader([]byte(body)))
91+
} else {
92+
file, errOpen := os.Open(bodyFile)
93+
if errOpen != nil {
94+
log.Fatalf("failed to open file %s, error %v", bodyFile, err)
95+
}
96+
req, err = http.NewRequest(method, reqURL.String(), file)
97+
}
98+
4799
if err != nil {
48100
log.Fatalf("failed to create request: %v", err)
49101
}
@@ -53,6 +105,11 @@ func main() {
53105
if body != "" {
54106
req.Header.Set("Content-Type", contentType)
55107
}
108+
if len(headers) > 0 {
109+
for _, h := range headers {
110+
req.Header.Set(h.key, h.value)
111+
}
112+
}
56113

57114
resp, err := client.Do(req)
58115
if err != nil {

0 commit comments

Comments
 (0)