-
Notifications
You must be signed in to change notification settings - Fork 717
/
example_test.go
241 lines (195 loc) · 6.08 KB
/
example_test.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) 2015-2024 Jeevanandam M. (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package resty_test
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"golang.org/x/net/proxy"
"github.com/go-resty/resty/v2"
)
type DropboxError struct {
Error string
}
type AuthSuccess struct {
/* variables */
}
type AuthError struct {
/* variables */
}
type Article struct {
Title string
Content string
Author string
Tags []string
}
type Error struct {
/* variables */
}
//
// Package Level examples
//
func Example_get() {
// Create a resty client
client := resty.New()
resp, err := client.R().Get("http://httpbin.org/get")
fmt.Printf("\nError: %v", err)
fmt.Printf("\nResponse Status Code: %v", resp.StatusCode())
fmt.Printf("\nResponse Status: %v", resp.Status())
fmt.Printf("\nResponse Body: %v", resp)
fmt.Printf("\nResponse Time: %v", resp.Time())
fmt.Printf("\nResponse Received At: %v", resp.ReceivedAt())
}
func Example_enhancedGet() {
// Create a resty client
client := resty.New()
resp, err := client.R().
SetQueryParams(map[string]string{
"page_no": "1",
"limit": "20",
"sort": "name",
"order": "asc",
"random": strconv.FormatInt(time.Now().Unix(), 10),
}).
SetHeader("Accept", "application/json").
SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
Get("/search_result")
printOutput(resp, err)
}
func Example_post() {
// Create a resty client
client := resty.New()
// POST JSON string
// No need to set content type, if you have client level setting
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"username":"testuser", "password":"testpass"}`).
SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}).
Post("https://myapp.com/login")
printOutput(resp, err)
// POST []byte array
// No need to set content type, if you have client level setting
resp1, err1 := client.R().
SetHeader("Content-Type", "application/json").
SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)).
SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}).
Post("https://myapp.com/login")
printOutput(resp1, err1)
// POST Struct, default is JSON content type. No need to set one
resp2, err2 := client.R().
SetBody(resty.User{Username: "testuser", Password: "testpass"}).
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}).
SetError(&AuthError{}). // or SetError(AuthError{}).
Post("https://myapp.com/login")
printOutput(resp2, err2)
// POST Map, default is JSON content type. No need to set one
resp3, err3 := client.R().
SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}).
SetError(&AuthError{}). // or SetError(AuthError{}).
Post("https://myapp.com/login")
printOutput(resp3, err3)
}
func Example_dropboxUpload() {
// For example: upload file to Dropbox
// POST of raw bytes for file upload.
fileBytes, _ := os.ReadFile("/Users/jeeva/mydocument.pdf")
// Create a resty client
client := resty.New()
// See we are not setting content-type header, since go-resty automatically detects Content-Type for you
resp, err := client.R().
SetBody(fileBytes). // resty autodetects content type
SetContentLength(true). // Dropbox expects this value
SetAuthToken("<your-auth-token>").
SetError(DropboxError{}).
Post("https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf") // you can use PUT method too dropbox supports it
// Output print
fmt.Printf("\nError: %v\n", err)
fmt.Printf("Time: %v\n", resp.Time())
fmt.Printf("Body: %v\n", resp)
}
func Example_put() {
// Create a resty client
client := resty.New()
// Just one sample of PUT, refer POST for more combination
// request goes as JSON content type
// No need to set auth token, error, if you have client level settings
resp, err := client.R().
SetBody(Article{
Title: "go-resty",
Content: "This is my article content, oh ya!",
Author: "Jeevanandam M",
Tags: []string{"article", "sample", "resty"},
}).
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
SetError(&Error{}). // or SetError(Error{}).
Put("https://myapp.com/article/1234")
printOutput(resp, err)
}
func Example_clientCertificates() {
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
if err != nil {
log.Fatalf("ERROR client certificate: %s", err)
}
// Create a resty client
client := resty.New()
client.SetCertificates(cert)
}
func Example_customRootCertificate() {
// Create a resty client
client := resty.New()
client.SetRootCertificate("/path/to/root/pemFile.pem")
}
//
// top level method examples
//
func ExampleNew() {
// Creating client1
client1 := resty.New()
resp1, err1 := client1.R().Get("http://httpbin.org/get")
fmt.Println(resp1, err1)
// Creating client2
client2 := resty.New()
resp2, err2 := client2.R().Get("http://httpbin.org/get")
fmt.Println(resp2, err2)
}
//
// Client object methods
//
func ExampleClient_SetCertificates() {
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
if err != nil {
log.Fatalf("ERROR client certificate: %s", err)
}
// Create a resty client
client := resty.New()
client.SetCertificates(cert)
}
//
// Resty Socks5 Proxy request
//
func Example_socks5Proxy() {
// create a dialer
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9150", nil, proxy.Direct)
if err != nil {
log.Fatalf("Unable to obtain proxy dialer: %v\n", err)
}
// create a transport
ptransport := &http.Transport{Dial: dialer.Dial}
// Create a resty client
client := resty.New()
// set transport into resty
client.SetTransport(ptransport)
resp, err := client.R().Get("http://check.torproject.org")
fmt.Println(err, resp)
}
func printOutput(resp *resty.Response, err error) {
fmt.Println(resp, err)
}