1
2
3
4
5 package resty_test
6
7 import (
8 "crypto/tls"
9 "fmt"
10 "io/ioutil"
11 "log"
12 "net/http"
13 "os"
14 "strconv"
15 "time"
16
17 "golang.org/x/net/proxy"
18
19 "github.com/go-resty/resty/v2"
20 )
21
22 type DropboxError struct {
23 Error string
24 }
25 type AuthSuccess struct {
26
27 }
28 type AuthError struct {
29
30 }
31 type Article struct {
32 Title string
33 Content string
34 Author string
35 Tags []string
36 }
37 type Error struct {
38
39 }
40
41
42
43
44
45 func Example_get() {
46
47 client := resty.New()
48
49 resp, err := client.R().Get("http://httpbin.org/get")
50
51 fmt.Printf("\nError: %v", err)
52 fmt.Printf("\nResponse Status Code: %v", resp.StatusCode())
53 fmt.Printf("\nResponse Status: %v", resp.Status())
54 fmt.Printf("\nResponse Body: %v", resp)
55 fmt.Printf("\nResponse Time: %v", resp.Time())
56 fmt.Printf("\nResponse Received At: %v", resp.ReceivedAt())
57 }
58
59 func Example_enhancedGet() {
60
61 client := resty.New()
62
63 resp, err := client.R().
64 SetQueryParams(map[string]string{
65 "page_no": "1",
66 "limit": "20",
67 "sort": "name",
68 "order": "asc",
69 "random": strconv.FormatInt(time.Now().Unix(), 10),
70 }).
71 SetHeader("Accept", "application/json").
72 SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
73 Get("/search_result")
74
75 printOutput(resp, err)
76 }
77
78 func Example_post() {
79
80 client := resty.New()
81
82
83
84 resp, err := client.R().
85 SetHeader("Content-Type", "application/json").
86 SetBody(`{"username":"testuser", "password":"testpass"}`).
87 SetResult(AuthSuccess{}).
88 Post("https://myapp.com/login")
89
90 printOutput(resp, err)
91
92
93
94 resp1, err1 := client.R().
95 SetHeader("Content-Type", "application/json").
96 SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)).
97 SetResult(AuthSuccess{}).
98 Post("https://myapp.com/login")
99
100 printOutput(resp1, err1)
101
102
103 resp2, err2 := client.R().
104 SetBody(resty.User{Username: "testuser", Password: "testpass"}).
105 SetResult(&AuthSuccess{}).
106 SetError(&AuthError{}).
107 Post("https://myapp.com/login")
108
109 printOutput(resp2, err2)
110
111
112 resp3, err3 := client.R().
113 SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
114 SetResult(&AuthSuccess{}).
115 SetError(&AuthError{}).
116 Post("https://myapp.com/login")
117
118 printOutput(resp3, err3)
119 }
120
121 func Example_dropboxUpload() {
122
123
124 file, _ := os.Open("/Users/jeeva/mydocument.pdf")
125 fileBytes, _ := ioutil.ReadAll(file)
126
127
128 client := resty.New()
129
130
131 resp, err := client.R().
132 SetBody(fileBytes).
133 SetContentLength(true).
134 SetAuthToken("<your-auth-token>").
135 SetError(DropboxError{}).
136 Post("https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf")
137
138
139 fmt.Printf("\nError: %v\n", err)
140 fmt.Printf("Time: %v\n", resp.Time())
141 fmt.Printf("Body: %v\n", resp)
142 }
143
144 func Example_put() {
145
146 client := resty.New()
147
148
149
150
151 resp, err := client.R().
152 SetBody(Article{
153 Title: "go-resty",
154 Content: "This is my article content, oh ya!",
155 Author: "Jeevanandam M",
156 Tags: []string{"article", "sample", "resty"},
157 }).
158 SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
159 SetError(&Error{}).
160 Put("https://myapp.com/article/1234")
161
162 printOutput(resp, err)
163 }
164
165 func Example_clientCertificates() {
166
167 cert, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
168 if err != nil {
169 log.Fatalf("ERROR client certificate: %s", err)
170 }
171
172
173 client := resty.New()
174
175 client.SetCertificates(cert)
176 }
177
178 func Example_customRootCertificate() {
179
180 client := resty.New()
181 client.SetRootCertificate("/path/to/root/pemFile.pem")
182 }
183
184
185
186
187
188 func ExampleNew() {
189
190 client1 := resty.New()
191 resp1, err1 := client1.R().Get("http://httpbin.org/get")
192 fmt.Println(resp1, err1)
193
194
195 client2 := resty.New()
196 resp2, err2 := client2.R().Get("http://httpbin.org/get")
197 fmt.Println(resp2, err2)
198 }
199
200
201
202
203
204 func ExampleClient_SetCertificates() {
205
206 cert, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
207 if err != nil {
208 log.Fatalf("ERROR client certificate: %s", err)
209 }
210
211
212 client := resty.New()
213
214 client.SetCertificates(cert)
215 }
216
217
218
219
220
221 func Example_socks5Proxy() {
222
223 dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9150", nil, proxy.Direct)
224 if err != nil {
225 log.Fatalf("Unable to obtain proxy dialer: %v\n", err)
226 }
227
228
229 ptransport := &http.Transport{Dial: dialer.Dial}
230
231
232 client := resty.New()
233
234
235 client.SetTransport(ptransport)
236
237 resp, err := client.R().Get("http://check.torproject.org")
238 fmt.Println(err, resp)
239 }
240
241 func printOutput(resp *resty.Response, err error) {
242 fmt.Println(resp, err)
243 }
244
View as plain text