| 1 |
[](https://travis-ci.org/franela/goreq) |
| 2 |
[](https://godoc.org/github.com/franela/goreq) |
| 3 |
|
| 4 |
GoReq |
| 5 |
======= |
| 6 |
|
| 7 |
Simple and sane HTTP request library for Go language. |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
**Table of Contents** |
| 12 |
|
| 13 |
- [Why GoReq?](#user-content-why-goreq) |
| 14 |
- [How do I install it?](#user-content-how-do-i-install-it) |
| 15 |
- [What can I do with it?](#user-content-what-can-i-do-with-it) |
| 16 |
- [Making requests with different methods](#user-content-making-requests-with-different-methods) |
| 17 |
- [GET](#user-content-get) |
| 18 |
- [Tags](#user-content-tags) |
| 19 |
- [POST](#user-content-post) |
| 20 |
- [Sending payloads in the Body](#user-content-sending-payloads-in-the-body) |
| 21 |
- [Specifiying request headers](#user-content-specifiying-request-headers) |
| 22 |
- [Sending Cookies](#cookie-support) |
| 23 |
- [Setting timeouts](#user-content-setting-timeouts) |
| 24 |
- [Using the Response and Error](#user-content-using-the-response-and-error) |
| 25 |
- [Receiving JSON](#user-content-receiving-json) |
| 26 |
- [Sending/Receiving Compressed Payloads](#user-content-sendingreceiving-compressed-payloads) |
| 27 |
- [Using gzip compression:](#user-content-using-gzip-compression) |
| 28 |
- [Using deflate compression:](#user-content-using-deflate-compression) |
| 29 |
- [Using compressed responses:](#user-content-using-compressed-responses) |
| 30 |
- [Proxy](#proxy) |
| 31 |
- [Debugging requests](#debug) |
| 32 |
- [Getting raw Request & Response](#getting-raw-request--response) |
| 33 |
- [TODO:](#user-content-todo) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
Why GoReq? |
| 38 |
========== |
| 39 |
|
| 40 |
Go has very nice native libraries that allows you to do lots of cool things. But sometimes those libraries are too low level, which means that to do a simple thing, like an HTTP Request, it takes some time. And if you want to do something as simple as adding a timeout to a request, you will end up writing several lines of code. |
| 41 |
|
| 42 |
This is why we think GoReq is useful. Because you can do all your HTTP requests in a very simple and comprehensive way, while enabling you to do more advanced stuff by giving you access to the native API. |
| 43 |
|
| 44 |
How do I install it? |
| 45 |
==================== |
| 46 |
|
| 47 |
```bash |
| 48 |
go get github.com/franela/goreq |
| 49 |
``` |
| 50 |
|
| 51 |
What can I do with it? |
| 52 |
====================== |
| 53 |
|
| 54 |
## Making requests with different methods |
| 55 |
|
| 56 |
#### GET |
| 57 |
```go |
| 58 |
res, err := goreq.Request{ Uri: "http://www.google.com" }.Do() |
| 59 |
``` |
| 60 |
|
| 61 |
GoReq default method is GET. |
| 62 |
|
| 63 |
You can also set value to GET method easily |
| 64 |
|
| 65 |
```go |
| 66 |
type Item struct { |
| 67 |
Limit int |
| 68 |
Skip int |
| 69 |
Fields string |
| 70 |
} |
| 71 |
|
| 72 |
item := Item { |
| 73 |
Limit: 3, |
| 74 |
Skip: 5, |
| 75 |
Fields: "Value", |
| 76 |
} |
| 77 |
|
| 78 |
res, err := goreq.Request{ |
| 79 |
Uri: "http://localhost:3000/", |
| 80 |
QueryString: item, |
| 81 |
}.Do() |
| 82 |
``` |
| 83 |
The sample above will send `http://localhost:3000/?limit=3&skip=5&fields=Value` |
| 84 |
|
| 85 |
Alternatively the `url` tag can be used in struct fields to customize encoding properties |
| 86 |
|
| 87 |
```go |
| 88 |
type Item struct { |
| 89 |
TheLimit int `url:"the_limit"` |
| 90 |
TheSkip string `url:"the_skip,omitempty"` |
| 91 |
TheFields string `url:"-"` |
| 92 |
} |
| 93 |
|
| 94 |
item := Item { |
| 95 |
TheLimit: 3, |
| 96 |
TheSkip: "", |
| 97 |
TheFields: "Value", |
| 98 |
} |
| 99 |
|
| 100 |
res, err := goreq.Request{ |
| 101 |
Uri: "http://localhost:3000/", |
| 102 |
QueryString: item, |
| 103 |
}.Do() |
| 104 |
``` |
| 105 |
The sample above will send `http://localhost:3000/?the_limit=3` |
| 106 |
|
| 107 |
|
| 108 |
QueryString also support url.Values |
| 109 |
|
| 110 |
```go |
| 111 |
item := url.Values{} |
| 112 |
item.Set("Limit", 3) |
| 113 |
item.Add("Field", "somefield") |
| 114 |
item.Add("Field", "someotherfield") |
| 115 |
|
| 116 |
res, err := goreq.Request{ |
| 117 |
Uri: "http://localhost:3000/", |
| 118 |
QueryString: item, |
| 119 |
}.Do() |
| 120 |
``` |
| 121 |
|
| 122 |
The sample above will send `http://localhost:3000/?limit=3&field=somefield&field=someotherfield` |
| 123 |
|
| 124 |
### Tags |
| 125 |
|
| 126 |
Struct field `url` tag is mainly used as the request parameter name. |
| 127 |
Tags can be comma separated multiple values, 1st value is for naming and rest has special meanings. |
| 128 |
|
| 129 |
- special tag for 1st value |
| 130 |
- `-`: value is ignored if set this |
| 131 |
|
| 132 |
- special tag for rest 2nd value |
| 133 |
- `omitempty`: zero-value is ignored if set this |
| 134 |
- `squash`: the fields of embedded struct is used for parameter |
| 135 |
|
| 136 |
#### Tag Examples |
| 137 |
|
| 138 |
```go |
| 139 |
type Place struct { |
| 140 |
Country string `url:"country"` |
| 141 |
City string `url:"city"` |
| 142 |
ZipCode string `url:"zipcode,omitempty"` |
| 143 |
} |
| 144 |
|
| 145 |
type Person struct { |
| 146 |
Place `url:",squash"` |
| 147 |
|
| 148 |
FirstName string `url:"first_name"` |
| 149 |
LastName string `url:"last_name"` |
| 150 |
Age string `url:"age,omitempty"` |
| 151 |
Password string `url:"-"` |
| 152 |
} |
| 153 |
|
| 154 |
johnbull := Person{ |
| 155 |
Place: Place{ // squash the embedded struct value |
| 156 |
Country: "UK", |
| 157 |
City: "London", |
| 158 |
ZipCode: "SW1", |
| 159 |
}, |
| 160 |
FirstName: "John", |
| 161 |
LastName: "Doe", |
| 162 |
Age: "35", |
| 163 |
Password: "my-secret", // ignored for parameter |
| 164 |
} |
| 165 |
|
| 166 |
goreq.Request{ |
| 167 |
Uri: "http://localhost/", |
| 168 |
QueryString: johnbull, |
| 169 |
}.Do() |
| 170 |
// => `http://localhost/?first_name=John&last_name=Doe&age=35&country=UK&city=London&zip_code=SW1` |
| 171 |
|
| 172 |
|
| 173 |
// age and zipcode will be ignored because of `omitempty` |
| 174 |
// but firstname isn't. |
| 175 |
samurai := Person{ |
| 176 |
Place: Place{ // squash the embedded struct value |
| 177 |
Country: "Japan", |
| 178 |
City: "Tokyo", |
| 179 |
}, |
| 180 |
LastName: "Yagyu", |
| 181 |
} |
| 182 |
|
| 183 |
goreq.Request{ |
| 184 |
Uri: "http://localhost/", |
| 185 |
QueryString: samurai, |
| 186 |
}.Do() |
| 187 |
// => `http://localhost/?first_name=&last_name=yagyu&country=Japan&city=Tokyo` |
| 188 |
``` |
| 189 |
|
| 190 |
|
| 191 |
#### POST |
| 192 |
|
| 193 |
```go |
| 194 |
res, err := goreq.Request{ Method: "POST", Uri: "http://www.google.com" }.Do() |
| 195 |
``` |
| 196 |
|
| 197 |
## Sending payloads in the Body |
| 198 |
|
| 199 |
You can send ```string```, ```Reader``` or ```interface{}``` in the body. The first two will be sent as text. The last one will be marshalled to JSON, if possible. |
| 200 |
|
| 201 |
```go |
| 202 |
type Item struct { |
| 203 |
Id int |
| 204 |
Name string |
| 205 |
} |
| 206 |
|
| 207 |
item := Item{ Id: 1111, Name: "foobar" } |
| 208 |
|
| 209 |
res, err := goreq.Request{ |
| 210 |
Method: "POST", |
| 211 |
Uri: "http://www.google.com", |
| 212 |
Body: item, |
| 213 |
}.Do() |
| 214 |
``` |
| 215 |
|
| 216 |
## Specifiying request headers |
| 217 |
|
| 218 |
We think that most of the times the request headers that you use are: ```Host```, ```Content-Type```, ```Accept``` and ```User-Agent```. This is why we decided to make it very easy to set these headers. |
| 219 |
|
| 220 |
```go |
| 221 |
res, err := goreq.Request{ |
| 222 |
Uri: "http://www.google.com", |
| 223 |
Host: "foobar.com", |
| 224 |
Accept: "application/json", |
| 225 |
ContentType: "application/json", |
| 226 |
UserAgent: "goreq", |
| 227 |
}.Do() |
| 228 |
``` |
| 229 |
|
| 230 |
But sometimes you need to set other headers. You can still do it. |
| 231 |
|
| 232 |
```go |
| 233 |
req := goreq.Request{ Uri: "http://www.google.com" } |
| 234 |
|
| 235 |
req.AddHeader("X-Custom", "somevalue") |
| 236 |
|
| 237 |
req.Do() |
| 238 |
``` |
| 239 |
|
| 240 |
Alternatively you can use the `WithHeader` function to keep the syntax short |
| 241 |
|
| 242 |
```go |
| 243 |
res, err = goreq.Request{ Uri: "http://www.google.com" }.WithHeader("X-Custom", "somevalue").Do() |
| 244 |
``` |
| 245 |
|
| 246 |
## Cookie support |
| 247 |
|
| 248 |
Cookies can be either set at the request level by sending a [CookieJar](http://golang.org/pkg/net/http/cookiejar/) in the `CookieJar` request field |
| 249 |
or you can use goreq's one-liner WithCookie method as shown below |
| 250 |
|
| 251 |
```go |
| 252 |
res, err := goreq.Request{ |
| 253 |
Uri: "http://www.google.com", |
| 254 |
}. |
| 255 |
WithCookie(&http.Cookie{Name: "c1", Value: "v1"}). |
| 256 |
Do() |
| 257 |
``` |
| 258 |
|
| 259 |
## Setting timeouts |
| 260 |
|
| 261 |
GoReq supports 2 kind of timeouts. A general connection timeout and a request specific one. By default the connection timeout is of 1 second. There is no default for request timeout, which means it will wait forever. |
| 262 |
|
| 263 |
You can change the connection timeout doing: |
| 264 |
|
| 265 |
```go |
| 266 |
goreq.SetConnectTimeout(100 * time.Millisecond) |
| 267 |
``` |
| 268 |
|
| 269 |
And specify the request timeout doing: |
| 270 |
|
| 271 |
```go |
| 272 |
res, err := goreq.Request{ |
| 273 |
Uri: "http://www.google.com", |
| 274 |
Timeout: 500 * time.Millisecond, |
| 275 |
}.Do() |
| 276 |
``` |
| 277 |
|
| 278 |
## Using the Response and Error |
| 279 |
|
| 280 |
GoReq will always return 2 values: a ```Response``` and an ```Error```. |
| 281 |
If ```Error``` is not ```nil``` it means that an error happened while doing the request and you shouldn't use the ```Response``` in any way. |
| 282 |
You can check what happened by getting the error message: |
| 283 |
|
| 284 |
```go |
| 285 |
fmt.Println(err.Error()) |
| 286 |
``` |
| 287 |
And to make it easy to know if it was a timeout error, you can ask the error or return it: |
| 288 |
|
| 289 |
```go |
| 290 |
if serr, ok := err.(*goreq.Error); ok { |
| 291 |
if serr.Timeout() { |
| 292 |
... |
| 293 |
} |
| 294 |
} |
| 295 |
return err |
| 296 |
``` |
| 297 |
|
| 298 |
If you don't get an error, you can safely use the ```Response```. |
| 299 |
|
| 300 |
```go |
| 301 |
res.Uri // return final URL location of the response (fulfilled after redirect was made) |
| 302 |
res.StatusCode // return the status code of the response |
| 303 |
res.Body // gives you access to the body |
| 304 |
res.Body.ToString() // will return the body as a string |
| 305 |
res.Header.Get("Content-Type") // gives you access to all the response headers |
| 306 |
``` |
| 307 |
Remember that you should **always** close `res.Body` if it's not `nil` |
| 308 |
|
| 309 |
## Receiving JSON |
| 310 |
|
| 311 |
GoReq will help you to receive and unmarshal JSON. |
| 312 |
|
| 313 |
```go |
| 314 |
type Item struct { |
| 315 |
Id int |
| 316 |
Name string |
| 317 |
} |
| 318 |
|
| 319 |
var item Item |
| 320 |
|
| 321 |
res.Body.FromJsonTo(&item) |
| 322 |
``` |
| 323 |
|
| 324 |
## Sending/Receiving Compressed Payloads |
| 325 |
GoReq supports gzip, deflate and zlib compression of requests' body and transparent decompression of responses provided they have a correct `Content-Encoding` header. |
| 326 |
|
| 327 |
##### Using gzip compression: |
| 328 |
```go |
| 329 |
res, err := goreq.Request{ |
| 330 |
Method: "POST", |
| 331 |
Uri: "http://www.google.com", |
| 332 |
Body: item, |
| 333 |
Compression: goreq.Gzip(), |
| 334 |
}.Do() |
| 335 |
``` |
| 336 |
##### Using deflate/zlib compression: |
| 337 |
```go |
| 338 |
res, err := goreq.Request{ |
| 339 |
Method: "POST", |
| 340 |
Uri: "http://www.google.com", |
| 341 |
Body: item, |
| 342 |
Compression: goreq.Deflate(), |
| 343 |
}.Do() |
| 344 |
``` |
| 345 |
##### Using compressed responses: |
| 346 |
If servers replies a correct and matching `Content-Encoding` header (gzip requires `Content-Encoding: gzip` and deflate `Content-Encoding: deflate`) goreq transparently decompresses the response so the previous example should always work: |
| 347 |
```go |
| 348 |
type Item struct { |
| 349 |
Id int |
| 350 |
Name string |
| 351 |
} |
| 352 |
res, err := goreq.Request{ |
| 353 |
Method: "POST", |
| 354 |
Uri: "http://www.google.com", |
| 355 |
Body: item, |
| 356 |
Compression: goreq.Gzip(), |
| 357 |
}.Do() |
| 358 |
var item Item |
| 359 |
res.Body.FromJsonTo(&item) |
| 360 |
``` |
| 361 |
If no `Content-Encoding` header is replied by the server GoReq will return the crude response. |
| 362 |
|
| 363 |
## Proxy |
| 364 |
If you need to use a proxy for your requests GoReq supports the standard `http_proxy` env variable as well as manually setting the proxy for each request |
| 365 |
|
| 366 |
```go |
| 367 |
res, err := goreq.Request{ |
| 368 |
Method: "GET", |
| 369 |
Proxy: "http://myproxy:myproxyport", |
| 370 |
Uri: "http://www.google.com", |
| 371 |
}.Do() |
| 372 |
``` |
| 373 |
|
| 374 |
### Proxy basic auth is also supported |
| 375 |
|
| 376 |
```go |
| 377 |
res, err := goreq.Request{ |
| 378 |
Method: "GET", |
| 379 |
Proxy: "http://user:pass@myproxy:myproxyport", |
| 380 |
Uri: "http://www.google.com", |
| 381 |
}.Do() |
| 382 |
``` |
| 383 |
|
| 384 |
## Debug |
| 385 |
If you need to debug your http requests, it can print the http request detail. |
| 386 |
|
| 387 |
```go |
| 388 |
res, err := goreq.Request{ |
| 389 |
Method: "GET", |
| 390 |
Uri: "http://www.google.com", |
| 391 |
Compression: goreq.Gzip(), |
| 392 |
ShowDebug: true, |
| 393 |
}.Do() |
| 394 |
fmt.Println(res, err) |
| 395 |
``` |
| 396 |
|
| 397 |
and it will print the log: |
| 398 |
``` |
| 399 |
GET / HTTP/1.1 |
| 400 |
Host: www.google.com |
| 401 |
Accept: |
| 402 |
Accept-Encoding: gzip |
| 403 |
Content-Encoding: gzip |
| 404 |
Content-Type: |
| 405 |
``` |
| 406 |
|
| 407 |
|
| 408 |
### Getting raw Request & Response |
| 409 |
|
| 410 |
To get the Request: |
| 411 |
|
| 412 |
```go |
| 413 |
req := goreq.Request{ |
| 414 |
Host: "foobar.com", |
| 415 |
} |
| 416 |
|
| 417 |
//req.Request will return a new instance of an http.Request so you can safely use it for something else |
| 418 |
request, _ := req.NewRequest() |
| 419 |
|
| 420 |
``` |
| 421 |
|
| 422 |
|
| 423 |
To get the Response: |
| 424 |
|
| 425 |
```go |
| 426 |
res, err := goreq.Request{ |
| 427 |
Method: "GET", |
| 428 |
Uri: "http://www.google.com", |
| 429 |
Compression: goreq.Gzip(), |
| 430 |
ShowDebug: true, |
| 431 |
}.Do() |
| 432 |
|
| 433 |
// res.Response will contain the original http.Response structure |
| 434 |
fmt.Println(res.Response, err) |
| 435 |
``` |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
TODO: |
| 441 |
----- |
| 442 |
|
| 443 |
We do have a couple of [issues](https://github.com/franela/goreq/issues) pending we'll be addressing soon. But feel free to |
| 444 |
contribute and send us PRs (with tests please :smile:). |