-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl.go
44 lines (38 loc) · 983 Bytes
/
curl.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
package main
import (
"fmt"
"strings"
"github.com/atotto/clipboard"
)
func exportAsCurl(req *request) {
// add -H
headers := ""
headersMap, err := req.req.Headers.Map()
if err != nil {
panic(err)
}
for key, value := range headersMap {
// Skip accept-encoding header to prefer plaintext response
if strings.ToLower(key) == "accept-encoding" {
continue
}
// Drop these
if strings.HasPrefix(key, "sec-") || strings.HasPrefix(key, ":") {
continue
}
headers += fmt.Sprintf(" -H '%s: %s'", escapeShell(key), escapeShell(value))
}
// add -d
body := ""
if req.req.PostData != nil {
body = fmt.Sprintf(" -d '%s'", escapeShell(*req.req.PostData))
}
// add -X
method := ""
if req.req.Method != "GET" && req.req.Method != "POST" {
method = fmt.Sprintf(" -X %s", req.req.Method)
}
// assemble command
cmd := fmt.Sprintf("curl%s%s%s '%s'", headers, body, method, req.req.URL)
clipboard.WriteAll(cmd)
}