...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package client
16
17 import (
18 "bytes"
19 "fmt"
20 "io/ioutil"
21 "net/http"
22 "os"
23 )
24
25 var (
26 cURLDebug = false
27 )
28
29 func EnablecURLDebug() {
30 cURLDebug = true
31 }
32
33 func DisablecURLDebug() {
34 cURLDebug = false
35 }
36
37
38
39
40
41 func printcURL(req *http.Request) error {
42 if !cURLDebug {
43 return nil
44 }
45 var (
46 command string
47 b []byte
48 err error
49 )
50
51 if req.URL != nil {
52 command = fmt.Sprintf("curl -X %s %s", req.Method, req.URL.String())
53 }
54
55 if req.Body != nil {
56 b, err = ioutil.ReadAll(req.Body)
57 if err != nil {
58 return err
59 }
60 command += fmt.Sprintf(" -d %q", string(b))
61 }
62
63 fmt.Fprintf(os.Stderr, "cURL Command: %s\n", command)
64
65
66 body := bytes.NewBuffer(b)
67 req.Body = ioutil.NopCloser(body)
68
69 return nil
70 }
71
View as plain text