Skip to content

Commit 745c365

Browse files
committed
Implement CURL logging
1 parent da3963e commit 745c365

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

curl.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package httplog
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func (l *log) curl() string {
9+
var r = l.Req
10+
var b strings.Builder
11+
12+
fmt.Fprintf(&b, "curl")
13+
if r.Method != "GET" && r.Method != "POST" {
14+
fmt.Fprintf(&b, " -X %s", r.Method)
15+
}
16+
17+
fmt.Fprintf(&b, " %s", singleQuoted(fmt.Sprintf("%s://%s%s", l.scheme(), r.Host, r.URL)))
18+
19+
if r.Method == "POST" {
20+
fmt.Fprintf(&b, " --data-raw %s", singleQuoted(l.ReqBody.String()))
21+
}
22+
23+
for name, vals := range r.Header {
24+
for _, val := range vals {
25+
fmt.Fprintf(&b, " -H %s", singleQuoted(fmt.Sprintf("%s: %s", name, val)))
26+
}
27+
}
28+
29+
return b.String()
30+
}
31+
32+
func (l *log) scheme() string {
33+
if l.Req.TLS != nil {
34+
return "https"
35+
}
36+
return "http"
37+
}
38+
39+
func singleQuoted(v string) string {
40+
return fmt.Sprintf("'%s'", strings.ReplaceAll(v, "'", `'\''`))
41+
}

0 commit comments

Comments
 (0)