Simple HTTP and REST client library for Go (inspired by Ruby rest-client)
Features section describes in detail about Resty capabilities
string, []byte, struct, map, slice and io.Reader tooContent-Typeio.Reader*http.Request instance may be accessed during middleware and request execution via Request.RawRequestRequest.RawRequest.GetBody()[]byte array - response.Body() OR Access as string - response.String()response.Time() and when we response.ReceivedAt()JSON and XML content typeJSON, if you supply struct/map without header Content-Typeapplication/problem+json & application/problem+xmlmultipart/form-dataRequest.SetContext supportedBasicAuth and Bearer tokenContentLength value for all request or particular requestcurl -o flag. See SetOutputDirectory & SetOutput.Timeout, RedirectPolicy, Proxy, TLSClientConfig, Transport, etc.Content-Type when response Content-Type header missing. Refer to #92resty.New()http.RoundTripper implementation, see SetTransportRequestAttempt value, and the Request object contains an Attempt attributeHTTP/2 and HTTP/1.1, also HTTP/3 can be used with Resty, see this commentRecommended to use go1.23 and above.
Initially Resty started supporting go modules since v1.10.0 release.
Starting Resty v2 and higher versions, it fully embraces go modules package release. It requires a Go version capable of understanding /vN suffixed imports:
Resty author also published following projects for Go Community.
struct.# Go Modules
require github.com/go-resty/resty/v2 v2.16.5
The following samples will assist you to become as comfortable as possible with resty library.
// Import resty into your code and refer it as `resty`.
import "github.com/go-resty/resty/v2"
// 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)
fmt.Println(" RequestAttempt:", ti.RequestAttempt)
fmt.Println(" RemoteAddr :", ti.RemoteAddr.String())
/* Output
Response Info:
Error : <nil>
Status Code: 200
Status : 200 OK
Proto : HTTP/2.0
Time : 457.034718ms
Received At: 2020-09-14 15:35:29.784681 -0700 PDT m=+0.458137045
Body :
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
"User-Agent": "go-resty/2.4.0 (https://github.com/go-resty/resty)",
"X-Amzn-Trace-Id": "Root=1-5f5ff031-000ff6292204aa6898e4de49"
},
"origin": "0.0.0.0",
"url": "https://httpbin.org/get"
}
Request Trace Info:
DNSLookup : 4.074657ms
ConnTime : 381.709936ms
TCPConnTime : 77.428048ms
TLSHandshake : 299.623597ms
ServerTime : 75.414703ms
ResponseTime : 79.337µs
TotalTime : 457.034718ms
IsConnReused : false
IsConnWasIdle : false
ConnIdleTime : 0s
RequestAttempt: 1
RemoteAddr : 3.221.81.55:443
*/
// Create a Resty Client
client := resty.New()
resp, err := client.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")
// Sample of using Request.SetQueryString method
resp, err := client.R().
SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more").
SetHeader("Accept", "application/json").
SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
Get("/show_product")
// If necessary, you can force response content type to tell Resty to parse a JSON response into your struct
resp, err := client.R().
SetResult(result).
ForceContentType("application/json").
Get("v2/alpine/manifests/latest")
```go // Create a Resty Client client := resty.New()
// POST JSON string
// No need to set content type, if you have client level setting
resp, err := client.R().
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
resp, err := client.R().
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 resp, err := client.R(). 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 resp, err := client.R(). 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, _ := os.ReadFile("/Users/jeeva/mydocument.pdf")
// See we are not setting content-type header, since go-resty automatically detects Content-Type for you resp, err := client.R(). SetBody(fileBytes). SetContentLength(true). // Dropbox expects this value SetAuthToken(""). 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
$ claude mcp add resty \
-- python -m otcore.mcp_server <graph>