pkg.re/essentialkaos/ek.v11@v12.41.0+incompatible/req/example_test.go (about) 1 package req 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleRequest_Do() { 17 var response = struct { 18 User string `json:"user"` 19 ID int `json:"id"` 20 Balance float64 `json:"balance"` 21 }{} 22 23 // Configure global engine 24 SetUserAgent("my-supper-app", "1.0") 25 SetDialTimeout(30.0) 26 SetRequestTimeout(30.0) 27 28 resp, err := Request{ 29 Method: GET, 30 URL: "https://my.domain.com", 31 Query: Query{ 32 "name": "Bob", 33 "id": 120, 34 "progress": 12.34, 35 }, 36 Headers: Headers{ 37 "My-Suppa-Header": "Test", 38 }, 39 ContentType: CONTENT_TYPE_JSON, 40 }.Do() 41 42 if err != nil { 43 fmt.Printf("Error: %v\n", err) 44 } 45 46 // print status code 47 fmt.Printf("Status code: %d\n", resp.StatusCode) 48 49 // decode JSON encoded response 50 err = resp.JSON(response) 51 52 if err != nil { 53 fmt.Printf("Error: %v\n", err) 54 } 55 56 // print response data 57 fmt.Printf( 58 "User: %s ID: %d Balance: %f\n", 59 response.User, response.ID, response.Balance, 60 ) 61 } 62 63 func ExampleRequest_Get() { 64 var response = struct { 65 User string `json:"user"` 66 ID int `json:"id"` 67 Balance float64 `json:"balance"` 68 }{} 69 70 resp, err := Request{URL: "https://my.domain.com"}.Get() 71 72 if err != nil { 73 fmt.Printf("Error: %v\n", err) 74 } 75 76 // decode json encoded response 77 err = resp.JSON(response) 78 79 if err != nil { 80 fmt.Printf("Error: %v\n", err) 81 } 82 83 // print response data 84 fmt.Printf( 85 "User: %s ID: %d Balance: %f\n", 86 response.User, response.ID, response.Balance, 87 ) 88 } 89 90 func ExampleRequest_Post() { 91 var request = struct { 92 UserID int `json:"user_id"` 93 }{ 94 UserID: 1234, 95 } 96 97 var response = struct { 98 User string `json:"user"` 99 ID int `json:"id"` 100 Balance float64 `json:"balance"` 101 }{} 102 103 // send post request with basic auth 104 resp, err := Request{ 105 URL: "https://my.domain.com", 106 Body: request, 107 Accept: CONTENT_TYPE_JSON, 108 ContentType: CONTENT_TYPE_JSON, 109 BasicAuthUsername: "someuser", 110 BasicAuthPassword: "somepass", 111 AutoDiscard: true, 112 }.Post() 113 114 if err != nil { 115 fmt.Printf("Error: %v\n", err) 116 } 117 118 // decode JSON encoded response 119 err = resp.JSON(response) 120 121 if err != nil { 122 fmt.Printf("Error: %v\n", err) 123 } 124 125 // print response data 126 fmt.Printf( 127 "User: %s ID: %d Balance: %f\n", 128 response.User, response.ID, response.Balance, 129 ) 130 } 131 132 func ExampleRequest_PostFile() { 133 extraFields := map[string]string{ 134 "user": "john", 135 "desc": "My photo", 136 } 137 138 // send multipart request with image 139 resp, err := Request{ 140 URL: "https://my.domain.com", 141 }.PostFile("/tmp/image.jpg", "file", extraFields) 142 143 if err != nil { 144 fmt.Printf("Error: %v\n", err) 145 return 146 } 147 148 if resp.StatusCode != 200 { 149 fmt.Printf("Can't upload file: %v\n", err) 150 return 151 } 152 153 fmt.Printf("File successfully uploaded!\n") 154 }