forked from keithballdotnet/resty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
187 lines (156 loc) · 4.91 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
package resty_test
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/go-resty/resty"
)
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() {
resp, err := resty.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 Recevied At: %v", resp.ReceivedAt)
}
func Example_enhancedGet() {
resp, err := resty.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() {
// POST JSON string
// No need to set content type, if you have client level setting
resp, err := resty.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 := resty.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 := resty.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 := resty.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.
file, _ := os.Open("/Users/jeeva/mydocument.pdf")
fileBytes, _ := ioutil.ReadAll(file)
// See we are not setting content-type header, since go-resty automatically detects Content-Type for you
resp, err := resty.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() {
// 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 := resty.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)
}
resty.SetCertificates(cert)
}
func Example_customRootCertificate() {
resty.SetRootCertificate("/path/to/root/pemFile.pem")
}
//
// top level method examples
//
func ExampleNew() {
// Creating client1
client1 := resty.New()
client1.R().Get("http://httpbin.org/get")
// Creating client2
client2 := resty.New()
client2.R().Get("http://httpbin.org/get")
}
//
// 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)
}
resty.SetCertificates(cert)
}
func printOutput(resp *resty.Response, err error) {
fmt.Println(resp, err)
}