Skip to content

Commit 231e19c

Browse files
authored
doc(readme): Add a note, GenerateCurlCommand needs to turn on EnableTrace (#817)
1 parent c646328 commit 231e19c

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

README.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* goroutine concurrent safe
6363
* Resty Client trace, see [Client.EnableTrace](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.EnableTrace) and [Request.EnableTrace](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.EnableTrace)
6464
* Since v2.4.0, trace info contains a `RequestAttempt` value, and the `Request` object contains an `Attempt` attribute
65+
* Supports `GenerateCurlCommand`(**You should turn on `EnableTrace`**, otherwise the curl command will not contain the body)
6566
* Debug mode - clean and informative logging presentation
6667
* Gzip - Go does it automatically also resty has fallback handling too
6768
* Works fine with `HTTP/2` and `HTTP/1.1`
@@ -122,18 +123,19 @@ The following samples will assist you to become as comfortable as possible with
122123
import "github.com/go-resty/resty/v2"
123124
```
124125

125-
#### Simple GET
126+
#### Simple POST
127+
>Refer: [debug_curl_test.go](https://github.com/go-resty/resty/blob/v2/examples/debug_curl_test.go)
126128
127129
```go
128130
// Create a Resty Client
129131
client := resty.New()
130132

131133
resp, err := client.R().
132-
EnableTrace().
133-
Get("https://httpbin.org/get")
134+
EnableTrace(). // You should turn on `EnableTrace`, otherwise the curl command will not contain the body
135+
SetBody(map[string]string{"name": "Alex"}).
136+
Post("https://httpbin.org/post")
134137
curlCmdExecuted := resp.Request.GenerateCurlCommand()
135138

136-
137139
// Explore curl command
138140
fmt.Println("Curl Command:\n ", curlCmdExecuted+"\n")
139141

@@ -166,27 +168,35 @@ fmt.Println(" RemoteAddr :", ti.RemoteAddr.String())
166168

167169
/* Output
168170
Curl Command:
169-
curl -X GET -H 'User-Agent: go-resty/2.12.0 (https://github.com/go-resty/resty)' https://httpbin.org/get
171+
curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: go-resty/2.14.0 (https://github.com/go-resty/resty)' -d '{"name":"Alex"}' https://httpbin.org/post
170172
171173
Response Info:
172174
Error : <nil>
173175
Status Code: 200
174176
Status : 200 OK
175177
Proto : HTTP/2.0
176178
Time : 457.034718ms
177-
Received At: 2020-09-14 15:35:29.784681 -0700 PDT m=+0.458137045
179+
Received At: 2024-08-09 13:02:57.187544 +0800 CST m=+1.304888501
178180
Body :
179-
{
180-
"args": {},
181-
"headers": {
182-
"Accept-Encoding": "gzip",
183-
"Host": "httpbin.org",
184-
"User-Agent": "go-resty/2.4.0 (https://github.com/go-resty/resty)",
185-
"X-Amzn-Trace-Id": "Root=1-5f5ff031-000ff6292204aa6898e4de49"
186-
},
187-
"origin": "0.0.0.0",
188-
"url": "https://httpbin.org/get"
189-
}
181+
{
182+
"args": {},
183+
"data": "{\"name\":\"Alex\"}",
184+
"files": {},
185+
"form": {},
186+
"headers": {
187+
"Accept-Encoding": "gzip",
188+
"Content-Length": "15",
189+
"Content-Type": "application/json",
190+
"Host": "httpbin.org",
191+
"User-Agent": "go-resty/2.14.0 (https://github.com/go-resty/resty)",
192+
"X-Amzn-Trace-Id": "Root=1-66b5a301-567c83c86562abd3092f5e19"
193+
},
194+
"json": {
195+
"name": "Alex"
196+
},
197+
"origin": "0.0.0.0",
198+
"url": "https://httpbin.org/post"
199+
}
190200
191201
Request Trace Info:
192202
DNSLookup : 4.074657ms

examples/debug_curl_test.go

+25-19
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ func TestGenerateUnexcutedCurl(t *testing.T) {
1515
ts := createHttpbinServer(0)
1616
defer ts.Close()
1717

18-
req := resty.New().R().SetBody(map[string]string{
19-
"name": "Alex",
20-
}).SetCookies(
21-
[]*http.Cookie{
22-
{Name: "count", Value: "1"},
23-
},
24-
)
18+
req := resty.New().R().
19+
SetBody(map[string]string{
20+
"name": "Alex",
21+
}).
22+
SetCookies(
23+
[]*http.Cookie{
24+
{Name: "count", Value: "1"},
25+
},
26+
)
2527

2628
curlCmdUnexecuted := req.GenerateCurlCommand()
2729

@@ -43,11 +45,13 @@ func TestGenerateExecutedCurl(t *testing.T) {
4345
data := map[string]string{
4446
"name": "Alex",
4547
}
46-
req := resty.New().R().SetBody(data).SetCookies(
47-
[]*http.Cookie{
48-
{Name: "count", Value: "1"},
49-
},
50-
)
48+
req := resty.New().R().
49+
SetBody(data).
50+
SetCookies(
51+
[]*http.Cookie{
52+
{Name: "count", Value: "1"},
53+
},
54+
)
5155

5256
url := ts.URL + "/post"
5357
resp, err := req.
@@ -77,13 +81,15 @@ func TestDebugModeCurl(t *testing.T) {
7781
defer restore()
7882

7983
// 2. Build request
80-
req := resty.New().R().SetBody(map[string]string{
81-
"name": "Alex",
82-
}).SetCookies(
83-
[]*http.Cookie{
84-
{Name: "count", Value: "1"},
85-
},
86-
)
84+
req := resty.New().R().
85+
SetBody(map[string]string{
86+
"name": "Alex",
87+
}).
88+
SetCookies(
89+
[]*http.Cookie{
90+
{Name: "count", Value: "1"},
91+
},
92+
)
8793

8894
// 3. Execute request: set debug mode
8995
url := ts.URL + "/post"

0 commit comments

Comments
 (0)