-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (120 loc) · 2.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"github.com/go-kratos/kratos/v2/encoding"
"github.com/spf13/pflag"
"golang.org/x/net/http2"
)
func main() {
// 定义命令行参数
url := pflag.StringP("url", "u", "", "URL to send the HTTP/2 request. eg: -u https://localhost/index")
method := pflag.StringP("method", "X", "GET", "HTTP method (GET, POST, etc.). eg: -X POST")
headers := pflag.StringArrayP("header", "H", []string{}, "HTTP headers in key:value format. eg: -H 'Content-Type: application/json','Accept: */*'")
//
data := pflag.StringP("data", "d", "", "Data to include in the request body.JSON and YAML formats are accepted. eg: -d '{\"foo\": \"bar\"}' OR -d @data.yml OR -d @data.json")
// 解析命令行参数
pflag.Parse()
// 验证必需参数
if *url == "" {
fmt.Println("Error: URL is required")
pflag.PrintDefaults()
os.Exit(1)
}
// 创建一个HTTP客户端
client := &http.Client{
Transport: transport(url),
}
// 创建请求
requestBody, err := readData(data)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(strings.ToUpper(*method), *url, requestBody)
if err != nil {
fmt.Println("Error creating request:", err)
os.Exit(1)
}
// 设置请求头
for _, header := range *headers {
parts := strings.SplitN(header, ":", 2)
if len(parts) == 2 {
req.Header.Set(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
os.Exit(1)
}
defer resp.Body.Close()
// 读取响应
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
os.Exit(1)
}
// 打印响应
fmt.Println("Status Code:", resp.Status)
fmt.Println("Response Body:", string(responseBody))
}
func readData(data *string) (*bytes.Buffer, error) {
var b []byte
if *data == "" {
return nil, nil
}
if strings.HasPrefix(*data, "@") {
bdata, err := os.ReadFile(strings.Replace(*data, "@", "", 1))
if err != nil {
return nil, err
}
b = bdata
} else {
b = []byte(*data)
}
codec := encoding.GetCodec(format(b))
if codec == nil {
return nil, fmt.Errorf("format not found")
}
tmpMap := map[string]interface{}{}
err := codec.Unmarshal(b, &tmpMap)
if err != nil {
return nil, err
}
dat, err := json.Marshal(tmpMap)
if err != nil {
return nil, err
}
// fmt.Printf("Data: %s - %s", b, dat)
return bytes.NewBuffer(dat), nil
}
func transport(URL *string) *http2.Transport {
u, err := url.Parse(*URL)
if err != nil {
log.Fatal("URL parse err", err)
}
switch u.Scheme {
case "http":
return &http2.Transport{
DisableCompression: true,
AllowHTTP: true,
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
},
}
default:
return &http2.Transport{}
}
}