2019-04-15 20:42:02 -04:00
< p align = "center" >
< h1 align = "center" > Resty< / h1 >
< p align = "center" > Simple HTTP and REST client library for Go (inspired by Ruby rest-client)< / p >
< p align = "center" > < a href = "#features" > Features< / a > section describes in detail about Resty capabilities< / p >
< / p >
< p align = "center" >
2020-06-12 05:36:54 -04:00
< p align = "center" > < a href = "https://travis-ci.org/go-resty/resty" > < img src = "https://travis-ci.org/go-resty/resty.svg?branch=master" alt = "Build Status" > < / a > < a href = "https://codecov.io/gh/go-resty/resty/branch/master" > < img src = "https://codecov.io/gh/go-resty/resty/branch/master/graph/badge.svg" alt = "Code Coverage" > < / a > < a href = "https://goreportcard.com/report/go-resty/resty" > < img src = "https://goreportcard.com/badge/go-resty/resty" alt = "Go Report Card" > < / a > < a href = "https://github.com/go-resty/resty/releases/latest" > < img src = "https://img.shields.io/badge/version-2.3.0-blue.svg" alt = "Release Version" > < / a > < a href = "https://pkg.go.dev/github.com/go-resty/resty/v2" > < img src = "https://godoc.org/github.com/go-resty/resty?status.svg" alt = "GoDoc" > < / a > < a href = "LICENSE" > < img src = "https://img.shields.io/github/license/go-resty/resty.svg" alt = "License" > < / a > < a href = "https://github.com/avelino/awesome-go" > < img src = "https://awesome.re/mentioned-badge.svg" alt = "Mentioned in Awesome Go" > < / a > < / p >
2019-04-15 20:42:02 -04:00
< / p >
< p align = "center" >
< h4 align = "center" > Resty Communication Channels< / h4 >
2020-06-12 05:36:54 -04:00
< p align = "center" >< a href = "https://gitter.im/go_resty/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" >< img src = "https://badges.gitter.im/go_resty/community.svg" alt = "Chat on Gitter - Resty Community" ></ a > < a href = "https://twitter.com/go_resty" >< img src = "https://img.shields.io/badge/twitter-@go__resty-55acee.svg" alt = "Twitter @go_resty " ></ a ></ p >
2019-04-15 20:42:02 -04:00
< / p >
## News
2020-06-12 05:36:54 -04:00
* v2.3.0 [released ](https://github.com/go-resty/resty/releases/tag/v2.3.0 ) and tagged on May 20, 2020.
* v2.0.0 [released ](https://github.com/go-resty/resty/releases/tag/v2.0.0 ) and tagged on Jul 16, 2019.
* v1.12.0 [released ](https://github.com/go-resty/resty/releases/tag/v1.12.0 ) and tagged on Feb 27, 2019.
2019-04-15 20:42:02 -04:00
* v1.0 released and tagged on Sep 25, 2017. - Resty's first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I'm very thankful to Resty users and its [contributors ](https://github.com/go-resty/resty/graphs/contributors ).
## Features
* GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.
* Simple and chainable methods for settings and request
2020-06-12 05:36:54 -04:00
* [Request ](https://godoc.org/github.com/go-resty/resty#Request ) Body can be `string` , `[]byte` , `struct` , `map` , `slice` and `io.Reader` too
2019-04-15 20:42:02 -04:00
* Auto detects `Content-Type`
* Buffer less processing for `io.Reader`
2020-06-12 05:36:54 -04:00
* Request Body can be read multiple times via `Request.RawRequest.GetBody()`
* [Response ](https://godoc.org/github.com/go-resty/resty#Response ) object gives you more possibility
2019-04-15 20:42:02 -04:00
* Access as `[]byte` array - `response.Body()` OR Access as `string` - `response.String()`
* Know your `response.Time()` and when we `response.ReceivedAt()`
* Automatic marshal and unmarshal for `JSON` and `XML` content type
* Default is `JSON` , if you supply `struct/map` without header `Content-Type`
* For auto-unmarshal, refer to -
2020-06-12 05:36:54 -04:00
- Success scenario [Request.SetResult() ](https://godoc.org/github.com/go-resty/resty#Request.SetResult ) and [Response.Result() ](https://godoc.org/github.com/go-resty/resty#Response.Result ).
- Error scenario [Request.SetError() ](https://godoc.org/github.com/go-resty/resty#Request.SetError ) and [Response.Error() ](https://godoc.org/github.com/go-resty/resty#Response.Error ).
2019-04-15 20:42:02 -04:00
- Supports [RFC7807 ](https://tools.ietf.org/html/rfc7807 ) - `application/problem+json` & `application/problem+xml`
* Easy to upload one or more file(s) via `multipart/form-data`
* Auto detects file content type
2020-06-12 05:36:54 -04:00
* Request URL [Path Params (aka URI Params) ](https://godoc.org/github.com/go-resty/resty#Request.SetPathParams )
2019-04-15 20:42:02 -04:00
* Backoff Retry Mechanism with retry condition function [reference ](retry_test.go )
2020-06-12 05:36:54 -04:00
* Resty client HTTP & REST [Request ](https://godoc.org/github.com/go-resty/resty#Client.OnBeforeRequest ) and [Response ](https://godoc.org/github.com/go-resty/resty#Client.OnAfterResponse ) middlewares
* `Request.SetContext` supported
2019-04-15 20:42:02 -04:00
* Authorization option of `BasicAuth` and `Bearer` token
* Set request `ContentLength` value for all request or particular request
2020-06-12 05:36:54 -04:00
* Custom [Root Certificates ](https://godoc.org/github.com/go-resty/resty#Client.SetRootCertificate ) and Client [Certificates ](https://godoc.org/github.com/go-resty/resty#Client.SetCertificates )
* Download/Save HTTP response directly into File, like `curl -o` flag. See [SetOutputDirectory ](https://godoc.org/github.com/go-resty/resty#Client.SetOutputDirectory ) & [SetOutput ](https://godoc.org/github.com/go-resty/resty#Request.SetOutput ).
2019-04-15 20:42:02 -04:00
* Cookies for your request and CookieJar support
* SRV Record based request instead of Host URL
* Client settings like `Timeout` , `RedirectPolicy` , `Proxy` , `TLSClientConfig` , `Transport` , etc.
2020-06-12 05:36:54 -04:00
* Optionally allows GET request with payload, see [SetAllowGetMethodPayload ](https://godoc.org/github.com/go-resty/resty#Client.SetAllowGetMethodPayload )
2019-04-15 20:42:02 -04:00
* Supports registering external JSON library into resty, see [how to use ](https://github.com/go-resty/resty/issues/76#issuecomment-314015250 )
* Exposes Response reader without reading response (no auto-unmarshaling) if need be, see [how to use ](https://github.com/go-resty/resty/issues/87#issuecomment-322100604 )
* Option to specify expected `Content-Type` when response `Content-Type` header missing. Refer to [#92 ](https://github.com/go-resty/resty/issues/92 )
* Resty design
* Have client level settings & options and also override at Request level if you want to
* Request and Response middlewares
* Create Multiple clients if you want to `resty.New()`
2020-06-12 05:36:54 -04:00
* Supports `http.RoundTripper` implementation, see [SetTransport ](https://godoc.org/github.com/go-resty/resty#Client.SetTransport )
2019-04-15 20:42:02 -04:00
* goroutine concurrent safe
2020-06-12 05:36:54 -04:00
* Resty Client trace, see [Client.EnableTrace ](https://godoc.org/github.com/go-resty/resty#Client.EnableTrace ) and [Request.EnableTrace ](https://godoc.org/github.com/go-resty/resty#Request.EnableTrace )
2019-04-15 20:42:02 -04:00
* Debug mode - clean and informative logging presentation
* Gzip - Go does it automatically also resty has fallback handling too
* Works fine with `HTTP/2` and `HTTP/1.1`
* [Bazel support ](#bazel-support )
2020-06-12 05:36:54 -04:00
* Easily mock Resty for testing, [for e.g. ](#mocking-http-requests-using-httpmock-library )
2019-04-15 20:42:02 -04:00
* Well tested client library
### Included Batteries
* Redirect Policies - see [how to use ](#redirect-policy )
* NoRedirectPolicy
* FlexibleRedirectPolicy
* DomainCheckRedirectPolicy
* etc. [more info ](redirect.go )
* Retry Mechanism [how to use ](#retries )
* Backoff Retry
* Conditional Retry
* SRV Record based request instead of Host URL [how to use ](resty_test.go#L1412 )
* etc (upcoming - throw your idea's [here ](https://github.com/go-resty/resty/issues )).
2020-06-12 05:36:54 -04:00
#### Supported Go Versions
2019-04-15 20:42:02 -04:00
2020-06-12 05:36:54 -04:00
Initially Resty started supporting `go modules` since `v1.10.0` release.
2019-04-15 20:42:02 -04:00
2020-06-12 05:36:54 -04:00
Starting Resty v2 and higher versions, it fully embraces [go modules ](https://github.com/golang/go/wiki/Modules ) package release. It requires a Go version capable of understanding `/vN` suffixed imports:
2019-04-15 20:42:02 -04:00
2020-06-12 05:36:54 -04:00
- 1.9.7+
- 1.10.3+
- 1.11+
2019-04-15 20:42:02 -04:00
## It might be beneficial for your project :smile:
Resty author also published following projects for Go Community.
* [aah framework ](https://aahframework.org ) - A secure, flexible, rapid Go web framework.
2020-06-12 05:36:54 -04:00
* [THUMBAI ](https://thumbai.app ) - Go Mod Repository, Go Vanity Service and Simple Proxy Server.
2019-04-15 20:42:02 -04:00
* [go-model ](https://github.com/jeevatkm/go-model ) - Robust & Easy to use model mapper and utility methods for Go `struct` .
2020-06-12 05:36:54 -04:00
## Installation
```bash
# Go Modules
require github.com/go-resty/resty/v2 v2.3.0
```
## Usage
2019-04-15 20:42:02 -04:00
2020-06-12 05:36:54 -04:00
The following samples will assist you to become as comfortable as possible with resty library.
2019-04-15 20:42:02 -04:00
```go
2020-06-12 05:36:54 -04:00
// Import resty into your code and refer it as `resty` .
import "github.com/go-resty/resty/v2"
2019-04-15 20:42:02 -04:00
```
#### Simple GET
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
resp, err := client.R().
EnableTrace().
Get("https://httpbin.org/get")
// Explore response object
fmt.Println("Response Info:")
fmt.Println("Error :", err)
fmt.Println("Status Code:", resp.StatusCode())
fmt.Println("Status :", resp.Status())
fmt.Println("Proto :", resp.Proto())
fmt.Println("Time :", resp.Time())
fmt.Println("Received At:", resp.ReceivedAt())
fmt.Println("Body :\n", resp)
fmt.Println()
// Explore trace info
fmt.Println("Request Trace Info:")
ti := resp.Request.TraceInfo()
fmt.Println("DNSLookup :", ti.DNSLookup)
fmt.Println("ConnTime :", ti.ConnTime)
fmt.Println("TCPConnTime :", ti.TCPConnTime)
fmt.Println("TLSHandshake :", ti.TLSHandshake)
fmt.Println("ServerTime :", ti.ServerTime)
fmt.Println("ResponseTime :", ti.ResponseTime)
fmt.Println("TotalTime :", ti.TotalTime)
fmt.Println("IsConnReused :", ti.IsConnReused)
fmt.Println("IsConnWasIdle:", ti.IsConnWasIdle)
fmt.Println("ConnIdleTime :", ti.ConnIdleTime)
2019-04-15 20:42:02 -04:00
/* Output
2020-06-12 05:36:54 -04:00
Response Info:
Error : < nil >
Status Code: 200
Status : 200 OK
Proto : HTTP/2.0
Time : 475.611189ms
Received At: 2020-05-19 00:11:06.828188 -0700 PDT m=+0.476510773
Body :
{
2019-04-15 20:42:02 -04:00
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
2020-06-12 05:36:54 -04:00
"User-Agent": "go-resty/2.3.0 (https://github.com/go-resty/resty)"
2019-04-15 20:42:02 -04:00
},
"origin": "0.0.0.0",
2020-06-12 05:36:54 -04:00
"url": "https://httpbin.org/get"
2019-04-15 20:42:02 -04:00
}
2020-06-12 05:36:54 -04:00
Request Trace Info:
DNSLookup : 4.870246ms
ConnTime : 393.95373ms
TCPConnTime : 78.360432ms
TLSHandshake : 310.032859ms
ServerTime : 81.648284ms
ResponseTime : 124.266µs
TotalTime : 475.611189ms
IsConnReused : false
IsConnWasIdle: false
ConnIdleTime : 0s
2019-04-15 20:42:02 -04:00
*/
```
#### Enhanced GET
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
resp, err := client.R().
2019-04-15 20:42:02 -04:00
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")
// Sample of using Request.SetQueryString method
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetQueryString("productId=232& template=fresh-sample& cat=resty& source=google& kw=buy a lot more").
SetHeader("Accept", "application/json").
SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
Get("/show_product")
```
#### Various POST method combinations
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// POST JSON string
// No need to set content type, if you have client level setting
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetHeader("Content-Type", "application/json").
SetBody(`{"username":"testuser", "password":"testpass"}`).
SetResult(& AuthSuccess{}). // or SetResult(AuthSuccess{}).
Post("https://myapp.com/login")
// POST []byte array
// No need to set content type, if you have client level setting
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetHeader("Content-Type", "application/json").
SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)).
SetResult(& AuthSuccess{}). // or SetResult(AuthSuccess{}).
Post("https://myapp.com/login")
// POST Struct, default is JSON content type. No need to set one
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetBody(User{Username: "testuser", Password: "testpass"}).
SetResult(& AuthSuccess{}). // or SetResult(AuthSuccess{}).
SetError(& AuthError{}). // or SetError(AuthError{}).
Post("https://myapp.com/login")
// POST Map, default is JSON content type. No need to set one
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
SetResult(& AuthSuccess{}). // or SetResult(AuthSuccess{}).
SetError(& AuthError{}). // or SetError(AuthError{}).
Post("https://myapp.com/login")
// POST of raw bytes for file upload. For example: upload file to Dropbox
fileBytes, _ := ioutil.ReadFile("/Users/jeeva/mydocument.pdf")
// See we are not setting content-type header, since go-resty automatically detects Content-Type for you
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetBody(fileBytes).
SetContentLength(true). // Dropbox expects this value
SetAuthToken("< your-auth-token > ").
SetError(& DropboxError{}). // or SetError(DropboxError{}).
Post("https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf") // for upload Dropbox supports PUT too
// Note: resty detects Content-Type for request body/payload if content type header is not set.
// * For struct and map data type defaults to 'application/json'
// * Fallback is plain text content type
```
#### Sample PUT
You can use various combinations of `PUT` method call like demonstrated for `POST` .
```go
// Note: This is one sample of PUT method usage, refer POST for more combination
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Request goes as JSON content type
// No need to set auth token, error, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
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")
```
#### Sample PATCH
You can use various combinations of `PATCH` method call like demonstrated for `POST` .
```go
// Note: This is one sample of PUT method usage, refer POST for more combination
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Request goes as JSON content type
// No need to set auth token, error, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetBody(Article{
Tags: []string{"new tag1", "new tag2"},
}).
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
SetError(& Error{}). // or SetError(Error{}).
Patch("https://myapp.com/articles/1234")
```
#### Sample DELETE, HEAD, OPTIONS
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// DELETE a article
// No need to set auth token, error, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
SetError(& Error{}). // or SetError(Error{}).
Delete("https://myapp.com/articles/1234")
// DELETE a articles with payload/body as a JSON string
// No need to set auth token, error, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
SetError(& Error{}). // or SetError(Error{}).
SetHeader("Content-Type", "application/json").
SetBody(`{article_ids: [1002, 1006, 1007, 87683, 45432] }`).
Delete("https://myapp.com/articles")
// HEAD of resource
// No need to set auth token, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
Head("https://myapp.com/videos/hi-res-video")
// OPTIONS of resource
// No need to set auth token, if you have client level settings
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
Options("https://myapp.com/servers/nyc-dc-01")
```
### Multipart File(s) upload
#### Using io.Reader
```go
profileImgBytes, _ := ioutil.ReadFile("/Users/jeeva/test-img.png")
notesBytes, _ := ioutil.ReadFile("/Users/jeeva/text-file.txt")
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFileReader("profile_img", "test-img.png", bytes.NewReader(profileImgBytes)).
SetFileReader("notes", "text-file.txt", bytes.NewReader(notesBytes)).
SetFormData(map[string]string{
"first_name": "Jeevanandam",
"last_name": "M",
}).
Post("http://myapp.com/upload")
```
#### Using File directly from Path
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Single file scenario
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFile("profile_img", "/Users/jeeva/test-img.png").
Post("http://myapp.com/upload")
// Multiple files scenario
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFiles(map[string]string{
"profile_img": "/Users/jeeva/test-img.png",
"notes": "/Users/jeeva/text-file.txt",
}).
Post("http://myapp.com/upload")
// Multipart of form fields and files
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFiles(map[string]string{
"profile_img": "/Users/jeeva/test-img.png",
"notes": "/Users/jeeva/text-file.txt",
}).
SetFormData(map[string]string{
"first_name": "Jeevanandam",
"last_name": "M",
"zip_code": "00001",
"city": "my city",
"access_token": "C6A79608-782F-4ED0-A11D-BD82FAD829CD",
}).
Post("http://myapp.com/profile")
```
#### Sample Form submission
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// just mentioning about POST as an example with simple flow
// User Login
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFormData(map[string]string{
"username": "jeeva",
"password": "mypass",
}).
Post("http://myapp.com/login")
// Followed by profile update
2020-06-12 05:36:54 -04:00
resp, err := client.R().
2019-04-15 20:42:02 -04:00
SetFormData(map[string]string{
"first_name": "Jeevanandam",
"last_name": "M",
"zip_code": "00001",
"city": "new city update",
}).
Post("http://myapp.com/profile")
// Multi value form data
criteria := url.Values{
"search_criteria": []string{"book", "glass", "pencil"},
}
2020-06-12 05:36:54 -04:00
resp, err := client.R().
SetFormDataFromValues(criteria).
2019-04-15 20:42:02 -04:00
Post("http://myapp.com/search")
```
#### Save HTTP Response into File
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Setting output directory path, If directory not exists then resty creates one!
// This is optional one, if you're planning using absoule path in
// `Request.SetOutput` and can used together.
2020-06-12 05:36:54 -04:00
client.SetOutputDirectory("/Users/jeeva/Downloads")
2019-04-15 20:42:02 -04:00
// HTTP response gets saved into file, similar to curl -o flag
2020-06-12 05:36:54 -04:00
_, err := client.R().
2019-04-15 20:42:02 -04:00
SetOutput("plugin/ReplyWithHeader-v5.1-beta.zip").
Get("http://bit.ly/1LouEKr")
// OR using absolute path
2020-06-12 05:36:54 -04:00
// Note: output directory path is not used for absolute path
_, err := client.R().
2019-04-15 20:42:02 -04:00
SetOutput("/MyDownloads/plugin/ReplyWithHeader-v5.1-beta.zip").
Get("http://bit.ly/1LouEKr")
```
#### Request URL Path Params
Resty provides easy to use dynamic request URL path params. Params can be set at client and request level. Client level params value can be overridden at request level.
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
client.R().SetPathParams(map[string]string{
2019-04-15 20:42:02 -04:00
"userId": "sample@sample.com",
"subAccountId": "100002",
}).
Get("/v1/users/{userId}/{subAccountId}/details")
// Result:
// Composed URL - /v1/users/sample@sample.com/100002/details
```
#### Request and Response Middleware
Resty provides middleware ability to manipulate for Request and Response. It is more flexible than callback approach.
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Registering Request Middleware
2020-06-12 05:36:54 -04:00
client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
2019-04-15 20:42:02 -04:00
// Now you have access to Client and current Request object
// manipulate it as per your need
return nil // if its success otherwise return error
})
// Registering Response Middleware
2020-06-12 05:36:54 -04:00
client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
2019-04-15 20:42:02 -04:00
// Now you have access to Client and current Response object
// manipulate it as per your need
return nil // if its success otherwise return error
})
```
#### Redirect Policy
Resty provides few ready to use redirect policy(s) also it supports multiple policies together.
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Assign Client Redirect Policy. Create one as per you need
2020-06-12 05:36:54 -04:00
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(15))
2019-04-15 20:42:02 -04:00
// Wanna multiple policies such as redirect count, domain name check, etc
2020-06-12 05:36:54 -04:00
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20),
2019-04-15 20:42:02 -04:00
resty.DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))
```
##### Custom Redirect Policy
Implement [RedirectPolicy ](redirect.go#L20 ) interface and register it with resty client. Have a look [redirect.go ](redirect.go ) for more information.
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Using raw func into resty.SetRedirectPolicy
2020-06-12 05:36:54 -04:00
client.SetRedirectPolicy(resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
2019-04-15 20:42:02 -04:00
// Implement your logic here
// return nil for continue redirect otherwise return error to stop/prevent redirect
return nil
}))
//---------------------------------------------------
// Using struct create more flexible redirect policy
type CustomRedirectPolicy struct {
// variables goes here
}
func (c *CustomRedirectPolicy) Apply(req *http.Request, via []*http.Request) error {
// Implement your logic here
// return nil for continue redirect otherwise return error to stop/prevent redirect
return nil
}
// Registering in resty
2020-06-12 05:36:54 -04:00
client.SetRedirectPolicy(CustomRedirectPolicy{/* initialize variables */})
2019-04-15 20:42:02 -04:00
```
#### Custom Root Certificates and Client Certificates
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Custom Root certificates, just supply .pem file.
// you can add one or more root certificates, its get appended
2020-06-12 05:36:54 -04:00
client.SetRootCertificate("/path/to/root/pemFile1.pem")
client.SetRootCertificate("/path/to/root/pemFile2.pem")
2019-04-15 20:42:02 -04:00
// ... and so on!
// Adding Client Certificates, you add one or more certificates
// Sample for creating certificate object
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert1, err := tls.LoadX509KeyPair("certs/client.pem", "certs/client.key")
if err != nil {
log.Fatalf("ERROR client certificate: %s", err)
}
// ...
// You add one or more certificates
2020-06-12 05:36:54 -04:00
client.SetCertificates(cert1, cert2, cert3)
```
#### Custom Root Certificates and Client Certificates from string
```go
// Custom Root certificates from string
// You can pass you certificates throught env variables as strings
// you can add one or more root certificates, its get appended
client.SetRootCertificateFromString("-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----")
client.SetRootCertificateFromString("-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----")
// ... and so on!
// Adding Client Certificates, you add one or more certificates
// Sample for creating certificate object
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert1, err := tls.X509KeyPair([]byte("-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----"), []byte("-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----"))
if err != nil {
log.Fatalf("ERROR client certificate: %s", err)
}
// ...
// You add one or more certificates
client.SetCertificates(cert1, cert2, cert3)
2019-04-15 20:42:02 -04:00
```
#### Proxy Settings - Client as well as at Request Level
Default `Go` supports Proxy via environment variable `HTTP_PROXY` . Resty provides support via `SetProxy` & `RemoveProxy` .
Choose as per your need.
**Client Level Proxy** settings applied to all the request
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Setting a Proxy URL and Port
2020-06-12 05:36:54 -04:00
client.SetProxy("http://proxyserver:8888")
2019-04-15 20:42:02 -04:00
// Want to remove proxy setting
2020-06-12 05:36:54 -04:00
client.RemoveProxy()
2019-04-15 20:42:02 -04:00
```
#### Retries
Resty uses [backoff ](http://www.awsarchitectureblog.com/2015/03/backoff.html )
to increase retry intervals after each attempt.
Usage example:
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Retries are configured per client
2020-06-12 05:36:54 -04:00
client.
2019-04-15 20:42:02 -04:00
// Set retry count to non zero to enable retries
SetRetryCount(3).
// You can override initial retry wait time.
// Default is 100 milliseconds.
SetRetryWaitTime(5 * time.Second).
// MaxWaitTime can be overridden as well.
// Default is 2 seconds.
2020-06-12 05:36:54 -04:00
SetRetryMaxWaitTime(20 * time.Second).
// SetRetryAfter sets callback to calculate wait time between retries.
// Default (nil) implies exponential backoff with jitter
SetRetryAfter(func(client *Client, resp *Response) (time.Duration, error) {
return 0, errors.New("quota exceeded")
})
2019-04-15 20:42:02 -04:00
```
Above setup will result in resty retrying requests returned non nil error up to
3 times with delay increased after each attempt.
You can optionally provide client with custom retry conditions:
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
client.AddRetryCondition(
// RetryConditionFunc type is for retry condition function
// input: non-nil Response OR request execution error
func(r *resty.Response, err error) bool {
return r.StatusCode() == http.StatusTooManyRequests
2019-04-15 20:42:02 -04:00
},
)
```
Above example will make resty retry requests ended with `429 Too Many Requests`
status code.
Multiple retry conditions can be added.
It is also possible to use `resty.Backoff(...)` to get arbitrary retry scenarios
implemented. [Reference ](retry_test.go ).
#### Allow GET request with Payload
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Allow GET request with Payload. This is disabled by default.
2020-06-12 05:36:54 -04:00
client.SetAllowGetMethodPayload(true)
2019-04-15 20:42:02 -04:00
```
#### Wanna Multiple Clients
```go
// Here you go!
// Client 1
client1 := resty.New()
client1.R().Get("http://httpbin.org")
// ...
// Client 2
client2 := resty.New()
client2.R().Head("http://httpbin.org")
// ...
// Bend it as per your need!!!
```
#### Remaining Client Settings & its Options
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Unique settings at Client level
//--------------------------------
// Enable debug mode
2020-06-12 05:36:54 -04:00
client.SetDebug(true)
2019-04-15 20:42:02 -04:00
// Assign Client TLSClientConfig
// One can set custom root-certificate. Refer: http://golang.org/pkg/crypto/tls/#example_Dial
2020-06-12 05:36:54 -04:00
client.SetTLSClientConfig(& tls.Config{ RootCAs: roots })
2019-04-15 20:42:02 -04:00
// or One can disable security check (https)
2020-06-12 05:36:54 -04:00
client.SetTLSClientConfig(& tls.Config{ InsecureSkipVerify: true })
2019-04-15 20:42:02 -04:00
// Set client timeout as per your need
2020-06-12 05:36:54 -04:00
client.SetTimeout(1 * time.Minute)
2019-04-15 20:42:02 -04:00
// You can override all below settings and options at request level if you want to
//--------------------------------------------------------------------------------
// Host URL for all request. So you can use relative URL in the request
2020-06-12 05:36:54 -04:00
client.SetHostURL("http://httpbin.org")
2019-04-15 20:42:02 -04:00
// Headers for all request
2020-06-12 05:36:54 -04:00
client.SetHeader("Accept", "application/json")
client.SetHeaders(map[string]string{
2019-04-15 20:42:02 -04:00
"Content-Type": "application/json",
"User-Agent": "My custom User Agent String",
})
// Cookies for all request
2020-06-12 05:36:54 -04:00
client.SetCookie(& http.Cookie{
2019-04-15 20:42:02 -04:00
Name:"go-resty",
Value:"This is cookie value",
Path: "/",
Domain: "sample.com",
MaxAge: 36000,
HttpOnly: true,
Secure: false,
})
2020-06-12 05:36:54 -04:00
client.SetCookies(cookies)
2019-04-15 20:42:02 -04:00
// URL query parameters for all request
2020-06-12 05:36:54 -04:00
client.SetQueryParam("user_id", "00001")
client.SetQueryParams(map[string]string{ // sample of those who use this manner
2019-04-15 20:42:02 -04:00
"api_key": "api-key-here",
"api_secert": "api-secert",
})
2020-06-12 05:36:54 -04:00
client.R().SetQueryString("productId=232& template=fresh-sample& cat=resty& source=google& kw=buy a lot more")
2019-04-15 20:42:02 -04:00
// Form data for all request. Typically used with POST and PUT
2020-06-12 05:36:54 -04:00
client.SetFormData(map[string]string{
2019-04-15 20:42:02 -04:00
"access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F",
})
// Basic Auth for all request
2020-06-12 05:36:54 -04:00
client.SetBasicAuth("myuser", "mypass")
2019-04-15 20:42:02 -04:00
// Bearer Auth Token for all request
2020-06-12 05:36:54 -04:00
client.SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F")
2019-04-15 20:42:02 -04:00
// Enabling Content length value for all request
2020-06-12 05:36:54 -04:00
client.SetContentLength(true)
2019-04-15 20:42:02 -04:00
// Registering global Error object structure for JSON/XML request
2020-06-12 05:36:54 -04:00
client.SetError(& Error{}) // or resty.SetError(Error{})
2019-04-15 20:42:02 -04:00
```
#### Unix Socket
```go
unixSocket := "/var/run/my_socket.sock"
// Create a Go's http.Transport so we can set it in resty.
transport := http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", unixSocket)
},
}
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
2019-04-15 20:42:02 -04:00
// Set the previous transport that we created, set the scheme of the communication to the
// socket and set the unixSocket as the HostURL.
2020-06-12 05:36:54 -04:00
client.SetTransport(& transport).SetScheme("http").SetHostURL(unixSocket)
2019-04-15 20:42:02 -04:00
// No need to write the host's URL on the request, just the path.
2020-06-12 05:36:54 -04:00
client.R().Get("/index.html")
2019-04-15 20:42:02 -04:00
```
#### Bazel support
Resty can be built, tested and depended upon via [Bazel ](https://bazel.build ).
For example, to run all tests:
```shell
bazel test :go_default_test
```
#### Mocking http requests using [httpmock](https://github.com/jarcoal/httpmock) library
In order to mock the http requests when testing your application you
could use the `httpmock` library.
When using the default resty client, you should pass the client to the library as follow:
```go
2020-06-12 05:36:54 -04:00
// Create a Resty Client
client := resty.New()
// Get the underlying HTTP Client and set it to Mock
httpmock.ActivateNonDefault(client.GetClient())
2019-04-15 20:42:02 -04:00
```
More detailed example of mocking resty http requests using ginko could be found [here ](https://github.com/jarcoal/httpmock#ginkgo--resty-example ).
## Versioning
2020-06-12 05:36:54 -04:00
Resty releases versions according to [Semantic Versioning ](http://semver.org )
2019-04-15 20:42:02 -04:00
2020-06-12 05:36:54 -04:00
* Resty v2 does not use `gopkg.in` service for library versioning.
* Resty fully adapted to `go mod` capabilities since `v1.10.0` release.
* Resty v1 series was using `gopkg.in` to provide versioning. `gopkg.in/resty.vX` points to appropriate tagged versions; `X` denotes version series number and it's a stable release for production use. For e.g. `gopkg.in/resty.v0` .
2019-04-15 20:42:02 -04:00
* Development takes place at the master branch. Although the code in master should always compile and test successfully, it might break API's. I aim to maintain backwards compatibility, but sometimes API's and behavior might be changed to fix a bug.
## Contribution
I would welcome your contribution! If you find any improvement or issue you want to fix, feel free to send a pull request, I like pull requests that include test cases for fix/enhancement. I have done my best to bring pretty good code coverage. Feel free to write tests.
BTW, I'd like to know what you think about `Resty` . Kindly open an issue or send me an email; it'd mean a lot to me.
## Creator
[Jeevanandam M. ](https://github.com/jeevatkm ) (jeeva@myjeeva.com)
2020-06-12 05:36:54 -04:00
## Core Team
Have a look on [Members ](https://github.com/orgs/go-resty/teams/core/members ) page.
2019-04-15 20:42:02 -04:00
## Contributors
Have a look on [Contributors ](https://github.com/go-resty/resty/graphs/contributors ) page.
## License
Resty released under MIT license, refer [LICENSE ](LICENSE ) file.