1// Copyright (c) 2015-2019 Jeevanandam M. (jeeva@myjeeva.com), All rights reserved. 2// resty source code and usage is governed by a MIT style 3// license that can be found in the LICENSE file. 4 5package resty_test 6 7import ( 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 22type DropboxError struct { 23 Error string 24} 25type AuthSuccess struct { 26 /* variables */ 27} 28type AuthError struct { 29 /* variables */ 30} 31type Article struct { 32 Title string 33 Content string 34 Author string 35 Tags []string 36} 37type Error struct { 38 /* variables */ 39} 40 41// 42// Package Level examples 43// 44 45func Example_get() { 46 // Create a resty client 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 59func Example_enhancedGet() { 60 // Create a resty client 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 78func Example_post() { 79 // Create a resty client 80 client := resty.New() 81 82 // POST JSON string 83 // No need to set content type, if you have client level setting 84 resp, err := client.R(). 85 SetHeader("Content-Type", "application/json"). 86 SetBody(`{"username":"testuser", "password":"testpass"}`). 87 SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}). 88 Post("https://myapp.com/login") 89 90 printOutput(resp, err) 91 92 // POST []byte array 93 // No need to set content type, if you have client level setting 94 resp1, err1 := client.R(). 95 SetHeader("Content-Type", "application/json"). 96 SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)). 97 SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}). 98 Post("https://myapp.com/login") 99 100 printOutput(resp1, err1) 101 102 // POST Struct, default is JSON content type. No need to set one 103 resp2, err2 := client.R(). 104 SetBody(resty.User{Username: "testuser", Password: "testpass"}). 105 SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). 106 SetError(&AuthError{}). // or SetError(AuthError{}). 107 Post("https://myapp.com/login") 108 109 printOutput(resp2, err2) 110 111 // POST Map, default is JSON content type. No need to set one 112 resp3, err3 := client.R(). 113 SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}). 114 SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). 115 SetError(&AuthError{}). // or SetError(AuthError{}). 116 Post("https://myapp.com/login") 117 118 printOutput(resp3, err3) 119} 120 121func Example_dropboxUpload() { 122 // For example: upload file to Dropbox 123 // POST of raw bytes for file upload. 124 file, _ := os.Open("/Users/jeeva/mydocument.pdf") 125 fileBytes, _ := ioutil.ReadAll(file) 126 127 // Create a resty client 128 client := resty.New() 129 130 // See we are not setting content-type header, since go-resty automatically detects Content-Type for you 131 resp, err := client.R(). 132 SetBody(fileBytes). // resty autodetects content type 133 SetContentLength(true). // Dropbox expects this value 134 SetAuthToken("<your-auth-token>"). 135 SetError(DropboxError{}). 136 Post("https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf") // you can use PUT method too dropbox supports it 137 138 // Output print 139 fmt.Printf("\nError: %v\n", err) 140 fmt.Printf("Time: %v\n", resp.Time()) 141 fmt.Printf("Body: %v\n", resp) 142} 143 144func Example_put() { 145 // Create a resty client 146 client := resty.New() 147 148 // Just one sample of PUT, refer POST for more combination 149 // request goes as JSON content type 150 // No need to set auth token, error, if you have client level settings 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{}). // or SetError(Error{}). 160 Put("https://myapp.com/article/1234") 161 162 printOutput(resp, err) 163} 164 165func Example_clientCertificates() { 166 // Parsing public/private key pair from a pair of files. The files must contain PEM encoded data. 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 // Create a resty client 173 client := resty.New() 174 175 client.SetCertificates(cert) 176} 177 178func Example_customRootCertificate() { 179 // Create a resty client 180 client := resty.New() 181 client.SetRootCertificate("/path/to/root/pemFile.pem") 182} 183 184// 185// top level method examples 186// 187 188func ExampleNew() { 189 // Creating client1 190 client1 := resty.New() 191 resp1, err1 := client1.R().Get("http://httpbin.org/get") 192 fmt.Println(resp1, err1) 193 194 // Creating client2 195 client2 := resty.New() 196 resp2, err2 := client2.R().Get("http://httpbin.org/get") 197 fmt.Println(resp2, err2) 198} 199 200// 201// Client object methods 202// 203 204func ExampleClient_SetCertificates() { 205 // Parsing public/private key pair from a pair of files. The files must contain PEM encoded data. 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 // Create a resty client 212 client := resty.New() 213 214 client.SetCertificates(cert) 215} 216 217// 218// Resty Socks5 Proxy request 219// 220 221func Example_socks5Proxy() { 222 // create a dialer 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 // create a transport 229 ptransport := &http.Transport{Dial: dialer.Dial} 230 231 // Create a resty client 232 client := resty.New() 233 234 // set transport into resty 235 client.SetTransport(ptransport) 236 237 resp, err := client.R().Get("http://check.torproject.org") 238 fmt.Println(err, resp) 239} 240 241func printOutput(resp *resty.Response, err error) { 242 fmt.Println(resp, err) 243} 244