{"id":13367359,"url":"https://github.com/Go-resty/resty","last_synced_at":"2025-03-12T18:32:23.586Z","repository":{"id":37405391,"uuid":"41561105","full_name":"go-resty/resty","owner":"go-resty","description":"Simple HTTP and REST client library for Go","archived":false,"fork":false,"pushed_at":"2024-10-28T04:09:08.000Z","size":3814,"stargazers_count":10067,"open_issues_count":29,"forks_count":710,"subscribers_count":99,"default_branch":"v2","last_synced_at":"2024-10-29T19:59:49.652Z","etag":null,"topics":["backoff","go","go-resty","golang","golang-library","hacktoberfest","hacktoberfest2021","http-client","middleware","redirects","rest-client","retry","srv-record"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-resty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-28T17:48:47.000Z","updated_at":"2024-10-29T19:11:28.000Z","dependencies_parsed_at":"2023-12-27T06:34:09.164Z","dependency_job_id":"ba2e6717-453e-4135-97ed-375becfa34d5","html_url":"https://github.com/go-resty/resty","commit_stats":{"total_commits":492,"total_committers":118,"mean_commits":4.169491525423729,"dds":"0.31707317073170727","last_synced_commit":"1c4960b1e4fd3a53fc71c8034ff5bd56f5519ef0"},"previous_names":[],"tags_count":80,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-resty%2Fresty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-resty%2Fresty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-resty%2Fresty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-resty%2Fresty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-resty","download_url":"https://codeload.github.com/go-resty/resty/tar.gz/refs/heads/v2","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243271596,"owners_count":20264485,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backoff","go","go-resty","golang","golang-library","hacktoberfest","hacktoberfest2021","http-client","middleware","redirects","rest-client","retry","srv-record"],"created_at":"2024-07-30T00:01:45.617Z","updated_at":"2025-03-12T18:32:23.577Z","avatar_url":"https://github.com/go-resty.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\n\u003ch1 align=\"center\"\u003eResty\u003c/h1\u003e\n\u003cp align=\"center\"\u003eSimple HTTP and REST client library for Go (inspired by Ruby rest-client)\u003c/p\u003e\n\u003cp align=\"center\"\u003e\u003ca href=\"#features\"\u003eFeatures\u003c/a\u003e section describes in detail about Resty capabilities\u003c/p\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003cp align=\"center\"\u003e\u003ca href=\"https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2\"\u003e\u003cimg src=\"https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2\" alt=\"Build Status\"\u003e\u003c/a\u003e \u003ca href=\"https://app.codecov.io/gh/go-resty/resty/tree/v2\"\u003e\u003cimg src=\"https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg\" alt=\"Code Coverage\"\u003e\u003c/a\u003e \u003ca href=\"https://goreportcard.com/report/go-resty/resty\"\u003e\u003cimg src=\"https://goreportcard.com/badge/go-resty/resty\" alt=\"Go Report Card\"\u003e\u003c/a\u003e \u003ca href=\"https://github.com/go-resty/resty/releases/latest\"\u003e\u003cimg src=\"https://img.shields.io/badge/version-2.16.5-blue.svg\" alt=\"Release Version\"\u003e\u003c/a\u003e \u003ca href=\"https://pkg.go.dev/github.com/go-resty/resty/v2\"\u003e\u003cimg src=\"https://pkg.go.dev/badge/github.com/go-resty/resty\" alt=\"GoDoc\"\u003e\u003c/a\u003e \u003ca href=\"LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/go-resty/resty.svg\" alt=\"License\"\u003e\u003c/a\u003e \u003ca href=\"https://github.com/avelino/awesome-go\"\u003e\u003cimg src=\"https://awesome.re/mentioned-badge.svg\" alt=\"Mentioned in Awesome Go\"\u003e\u003c/a\u003e\u003c/p\u003e\n\u003c/p\u003e\n\n## News\n\n  * v2.16.5 [released](https://github.com/go-resty/resty/releases/tag/v2.16.5) and tagged on Jan 22, 2025.\n  * v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019.\n  * v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019.\n  * 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).\n\n## Features\n\n  * GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.\n  * Simple and chainable methods for settings and request\n  * [Request](https://pkg.go.dev/github.com/go-resty/resty/v2#Request) Body can be `string`, `[]byte`, `struct`, `map`, `slice` and `io.Reader` too\n    * Auto detects `Content-Type`\n    * Buffer less processing for `io.Reader`\n    * Native `*http.Request` instance may be accessed during middleware and request execution via `Request.RawRequest`\n    * Request Body can be read multiple times via `Request.RawRequest.GetBody()`\n  * [Response](https://pkg.go.dev/github.com/go-resty/resty/v2#Response) object gives you more possibility\n    * Access as `[]byte` array - `response.Body()` OR Access as `string` - `response.String()`\n    * Know your `response.Time()` and when we `response.ReceivedAt()`\n  * Automatic marshal and unmarshal for `JSON` and `XML` content type\n    * Default is `JSON`, if you supply `struct/map` without header `Content-Type`\n    * For auto-unmarshal, refer to -\n        - Success scenario [Request.SetResult()](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.SetResult) and [Response.Result()](https://pkg.go.dev/github.com/go-resty/resty/v2#Response.Result).\n        - Error scenario [Request.SetError()](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.SetError) and [Response.Error()](https://pkg.go.dev/github.com/go-resty/resty/v2#Response.Error).\n        - Supports [RFC7807](https://tools.ietf.org/html/rfc7807) - `application/problem+json` \u0026 `application/problem+xml`\n    * Resty provides an option to override [JSON Marshal/Unmarshal and XML Marshal/Unmarshal](#override-json--xml-marshalunmarshal)\n  * Easy to upload one or more file(s) via `multipart/form-data`\n    * Auto detects file content type\n  * Request URL [Path Params (aka URI Params)](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.SetPathParams)\n  * Backoff Retry Mechanism with retry condition function [reference](retry_test.go)\n  * Resty client HTTP \u0026 REST [Request](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.OnBeforeRequest) and [Response](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.OnAfterResponse) middlewares\n  * `Request.SetContext` supported\n  * Authorization option of `BasicAuth` and `Bearer` token\n  * Set request `ContentLength` value for all request or particular request\n  * Custom [Root Certificates](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.SetRootCertificate) and Client [Certificates](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.SetCertificates)\n  * Download/Save HTTP response directly into File, like `curl -o` flag. See [SetOutputDirectory](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.SetOutputDirectory) \u0026 [SetOutput](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.SetOutput).\n  * Cookies for your request and CookieJar support\n  * SRV Record based request instead of Host URL\n  * Client settings like `Timeout`, `RedirectPolicy`, `Proxy`, `TLSClientConfig`, `Transport`, etc.\n  * Optionally allows GET request with payload, see [SetAllowGetMethodPayload](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.SetAllowGetMethodPayload)\n  * Supports registering external JSON library into resty, see [how to use](https://github.com/go-resty/resty/issues/76#issuecomment-314015250)\n  * 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)\n  * Option to specify expected `Content-Type` when response `Content-Type` header missing. Refer to [#92](https://github.com/go-resty/resty/issues/92)\n  * Resty design\n    * Have client level settings \u0026 options and also override at Request level if you want to\n    * Request and Response middleware\n    * Create Multiple clients if you want to `resty.New()`\n    * Supports `http.RoundTripper` implementation, see [SetTransport](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.SetTransport)\n    * goroutine concurrent safe\n    * Resty Client trace, see [Client.EnableTrace](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.EnableTrace) and [Request.EnableTrace](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.EnableTrace)\n      * Since v2.4.0, trace info contains a `RequestAttempt` value, and the `Request` object contains an `Attempt` attribute\n    * Supports on-demand CURL command generation, see [Client.EnableGenerateCurlOnDebug](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.EnableGenerateCurlOnDebug), [Request.EnableGenerateCurlOnDebug](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.EnableGenerateCurlOnDebug). It requires debug mode to be enabled.\n    * Debug mode - clean and informative logging presentation\n    * Gzip - Go does it automatically also resty has fallback handling too\n    * Works fine with `HTTP/2` and `HTTP/1.1`, also `HTTP/3` can be used with Resty, see this [comment](https://github.com/go-resty/resty/issues/846#issuecomment-2329696110)\n  * [Bazel support](#bazel-support)\n  * Easily mock Resty for testing, [for e.g.](#mocking-http-requests-using-httpmock-library)\n  * Well tested client library\n\n### Included Batteries\n\n  * Redirect Policies - see [how to use](#redirect-policy)\n    * NoRedirectPolicy\n    * FlexibleRedirectPolicy\n    * DomainCheckRedirectPolicy\n    * etc. [more info](redirect.go)\n  * Retry Mechanism [how to use](#retries)\n    * Backoff Retry\n    * Conditional Retry\n    * Since v2.6.0, Retry Hooks - [Client](https://pkg.go.dev/github.com/go-resty/resty/v2#Client.AddRetryHook), [Request](https://pkg.go.dev/github.com/go-resty/resty/v2#Request.AddRetryHook)\n  * SRV Record based request instead of Host URL [how to use](resty_test.go#L1412)\n  * etc (upcoming - throw your idea's [here](https://github.com/go-resty/resty/issues)).\n\n\n#### Supported Go Versions\n\nRecommended to use `go1.20` and above.\n\nInitially Resty started supporting `go modules` since `v1.10.0` release.\n\nStarting 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:\n\n- 1.9.7+\n- 1.10.3+\n- 1.11+\n\n\n## It might be beneficial for your project :smile:\n\nResty author also published following projects for Go Community.\n\n  * [go-model](https://github.com/jeevatkm/go-model) - Robust \u0026 Easy to use model mapper and utility methods for Go `struct`.\n\n\n## Installation\n\n```bash\n# Go Modules\nrequire github.com/go-resty/resty/v2 v2.16.5\n```\n\n## Usage\n\nThe following samples will assist you to become as comfortable as possible with resty library.\n\n```go\n// Import resty into your code and refer it as `resty`.\nimport \"github.com/go-resty/resty/v2\"\n```\n\n#### Simple GET\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nresp, err := client.R().\n    EnableTrace().\n    Get(\"https://httpbin.org/get\")\n\n// Explore response object\nfmt.Println(\"Response Info:\")\nfmt.Println(\"  Error      :\", err)\nfmt.Println(\"  Status Code:\", resp.StatusCode())\nfmt.Println(\"  Status     :\", resp.Status())\nfmt.Println(\"  Proto      :\", resp.Proto())\nfmt.Println(\"  Time       :\", resp.Time())\nfmt.Println(\"  Received At:\", resp.ReceivedAt())\nfmt.Println(\"  Body       :\\n\", resp)\nfmt.Println()\n\n// Explore trace info\nfmt.Println(\"Request Trace Info:\")\nti := resp.Request.TraceInfo()\nfmt.Println(\"  DNSLookup     :\", ti.DNSLookup)\nfmt.Println(\"  ConnTime      :\", ti.ConnTime)\nfmt.Println(\"  TCPConnTime   :\", ti.TCPConnTime)\nfmt.Println(\"  TLSHandshake  :\", ti.TLSHandshake)\nfmt.Println(\"  ServerTime    :\", ti.ServerTime)\nfmt.Println(\"  ResponseTime  :\", ti.ResponseTime)\nfmt.Println(\"  TotalTime     :\", ti.TotalTime)\nfmt.Println(\"  IsConnReused  :\", ti.IsConnReused)\nfmt.Println(\"  IsConnWasIdle :\", ti.IsConnWasIdle)\nfmt.Println(\"  ConnIdleTime  :\", ti.ConnIdleTime)\nfmt.Println(\"  RequestAttempt:\", ti.RequestAttempt)\nfmt.Println(\"  RemoteAddr    :\", ti.RemoteAddr.String())\n\n/* Output\nResponse Info:\n  Error      : \u003cnil\u003e\n  Status Code: 200\n  Status     : 200 OK\n  Proto      : HTTP/2.0\n  Time       : 457.034718ms\n  Received At: 2020-09-14 15:35:29.784681 -0700 PDT m=+0.458137045\n  Body       :\n  {\n    \"args\": {},\n    \"headers\": {\n      \"Accept-Encoding\": \"gzip\",\n      \"Host\": \"httpbin.org\",\n      \"User-Agent\": \"go-resty/2.4.0 (https://github.com/go-resty/resty)\",\n      \"X-Amzn-Trace-Id\": \"Root=1-5f5ff031-000ff6292204aa6898e4de49\"\n    },\n    \"origin\": \"0.0.0.0\",\n    \"url\": \"https://httpbin.org/get\"\n  }\n\nRequest Trace Info:\n  DNSLookup     : 4.074657ms\n  ConnTime      : 381.709936ms\n  TCPConnTime   : 77.428048ms\n  TLSHandshake  : 299.623597ms\n  ServerTime    : 75.414703ms\n  ResponseTime  : 79.337µs\n  TotalTime     : 457.034718ms\n  IsConnReused  : false\n  IsConnWasIdle : false\n  ConnIdleTime  : 0s\n  RequestAttempt: 1\n  RemoteAddr    : 3.221.81.55:443\n*/\n```\n\n#### Enhanced GET\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nresp, err := client.R().\n      SetQueryParams(map[string]string{\n          \"page_no\": \"1\",\n          \"limit\": \"20\",\n          \"sort\":\"name\",\n          \"order\": \"asc\",\n          \"random\":strconv.FormatInt(time.Now().Unix(), 10),\n      }).\n      SetHeader(\"Accept\", \"application/json\").\n      SetAuthToken(\"BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F\").\n      Get(\"/search_result\")\n\n\n// Sample of using Request.SetQueryString method\nresp, err := client.R().\n      SetQueryString(\"productId=232\u0026template=fresh-sample\u0026cat=resty\u0026source=google\u0026kw=buy a lot more\").\n      SetHeader(\"Accept\", \"application/json\").\n      SetAuthToken(\"BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F\").\n      Get(\"/show_product\")\n\n\n// If necessary, you can force response content type to tell Resty to parse a JSON response into your struct\nresp, err := client.R().\n      SetResult(result).\n      ForceContentType(\"application/json\").\n      Get(\"v2/alpine/manifests/latest\")\n```\n\n#### Various POST method combinations\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// POST JSON string\n// No need to set content type, if you have client level setting\nresp, err := client.R().\n      SetHeader(\"Content-Type\", \"application/json\").\n      SetBody(`{\"username\":\"testuser\", \"password\":\"testpass\"}`).\n      SetResult(\u0026AuthSuccess{}).    // or SetResult(AuthSuccess{}).\n      Post(\"https://myapp.com/login\")\n\n// POST []byte array\n// No need to set content type, if you have client level setting\nresp, err := client.R().\n      SetHeader(\"Content-Type\", \"application/json\").\n      SetBody([]byte(`{\"username\":\"testuser\", \"password\":\"testpass\"}`)).\n      SetResult(\u0026AuthSuccess{}).    // or SetResult(AuthSuccess{}).\n      Post(\"https://myapp.com/login\")\n\n// POST Struct, default is JSON content type. No need to set one\nresp, err := client.R().\n      SetBody(User{Username: \"testuser\", Password: \"testpass\"}).\n      SetResult(\u0026AuthSuccess{}).    // or SetResult(AuthSuccess{}).\n      SetError(\u0026AuthError{}).       // or SetError(AuthError{}).\n      Post(\"https://myapp.com/login\")\n\n// POST Map, default is JSON content type. No need to set one\nresp, err := client.R().\n      SetBody(map[string]interface{}{\"username\": \"testuser\", \"password\": \"testpass\"}).\n      SetResult(\u0026AuthSuccess{}).    // or SetResult(AuthSuccess{}).\n      SetError(\u0026AuthError{}).       // or SetError(AuthError{}).\n      Post(\"https://myapp.com/login\")\n\n// POST of raw bytes for file upload. For example: upload file to Dropbox\nfileBytes, _ := os.ReadFile(\"/Users/jeeva/mydocument.pdf\")\n\n// See we are not setting content-type header, since go-resty automatically detects Content-Type for you\nresp, err := client.R().\n      SetBody(fileBytes).\n      SetContentLength(true).          // Dropbox expects this value\n      SetAuthToken(\"\u003cyour-auth-token\u003e\").\n      SetError(\u0026DropboxError{}).       // or SetError(DropboxError{}).\n      Post(\"https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf\") // for upload Dropbox supports PUT too\n\n// Note: resty detects Content-Type for request body/payload if content type header is not set.\n//   * For struct and map data type defaults to 'application/json'\n//   * Fallback is plain text content type\n```\n\n#### Sample PUT\n\nYou can use various combinations of `PUT` method call like demonstrated for `POST`.\n\n```go\n// Note: This is one sample of PUT method usage, refer POST for more combination\n\n// Create a Resty Client\nclient := resty.New()\n\n// Request goes as JSON content type\n// No need to set auth token, error, if you have client level settings\nresp, err := client.R().\n      SetBody(Article{\n        Title: \"go-resty\",\n        Content: \"This is my article content, oh ya!\",\n        Author: \"Jeevanandam M\",\n        Tags: []string{\"article\", \"sample\", \"resty\"},\n      }).\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      SetError(\u0026Error{}).       // or SetError(Error{}).\n      Put(\"https://myapp.com/article/1234\")\n```\n\n#### Sample PATCH\n\nYou can use various combinations of `PATCH` method call like demonstrated for `POST`.\n\n```go\n// Note: This is one sample of PUT method usage, refer POST for more combination\n\n// Create a Resty Client\nclient := resty.New()\n\n// Request goes as JSON content type\n// No need to set auth token, error, if you have client level settings\nresp, err := client.R().\n      SetBody(Article{\n        Tags: []string{\"new tag1\", \"new tag2\"},\n      }).\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      SetError(\u0026Error{}).       // or SetError(Error{}).\n      Patch(\"https://myapp.com/articles/1234\")\n```\n\n#### Sample DELETE, HEAD, OPTIONS\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// DELETE a article\n// No need to set auth token, error, if you have client level settings\nresp, err := client.R().\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      SetError(\u0026Error{}).       // or SetError(Error{}).\n      Delete(\"https://myapp.com/articles/1234\")\n\n// DELETE a articles with payload/body as a JSON string\n// No need to set auth token, error, if you have client level settings\nresp, err := client.R().\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      SetError(\u0026Error{}).       // or SetError(Error{}).\n      SetHeader(\"Content-Type\", \"application/json\").\n      SetBody(`{article_ids: [1002, 1006, 1007, 87683, 45432] }`).\n      Delete(\"https://myapp.com/articles\")\n\n// HEAD of resource\n// No need to set auth token, if you have client level settings\nresp, err := client.R().\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      Head(\"https://myapp.com/videos/hi-res-video\")\n\n// OPTIONS of resource\n// No need to set auth token, if you have client level settings\nresp, err := client.R().\n      SetAuthToken(\"C6A79608-782F-4ED0-A11D-BD82FAD829CD\").\n      Options(\"https://myapp.com/servers/nyc-dc-01\")\n```\n\n#### Override JSON \u0026 XML Marshal/Unmarshal\n\nUser could register choice of JSON/XML library into resty or write your own. By default resty registers standard `encoding/json` and `encoding/xml` respectively.\n```go\n// Example of registering json-iterator\nimport jsoniter \"github.com/json-iterator/go\"\n\njson := jsoniter.ConfigCompatibleWithStandardLibrary\n\nclient := resty.New().\n    SetJSONMarshaler(json.Marshal).\n    SetJSONUnmarshaler(json.Unmarshal)\n\n// similarly user could do for XML too with -\nclient.SetXMLMarshaler(xml.Marshal).\n    SetXMLUnmarshaler(xml.Unmarshal)\n```\n\n### Multipart File(s) upload\n\n#### Using io.Reader\n\n```go\nprofileImgBytes, _ := os.ReadFile(\"/Users/jeeva/test-img.png\")\nnotesBytes, _ := os.ReadFile(\"/Users/jeeva/text-file.txt\")\n\n// Create a Resty Client\nclient := resty.New()\n\nresp, err := client.R().\n      SetFileReader(\"profile_img\", \"test-img.png\", bytes.NewReader(profileImgBytes)).\n      SetFileReader(\"notes\", \"text-file.txt\", bytes.NewReader(notesBytes)).\n      SetFormData(map[string]string{\n          \"first_name\": \"Jeevanandam\",\n          \"last_name\": \"M\",\n      }).\n      Post(\"http://myapp.com/upload\")\n```\n\n#### Using File directly from Path\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Single file scenario\nresp, err := client.R().\n      SetFile(\"profile_img\", \"/Users/jeeva/test-img.png\").\n      Post(\"http://myapp.com/upload\")\n\n// Multiple files scenario\nresp, err := client.R().\n      SetFiles(map[string]string{\n        \"profile_img\": \"/Users/jeeva/test-img.png\",\n        \"notes\": \"/Users/jeeva/text-file.txt\",\n      }).\n      Post(\"http://myapp.com/upload\")\n\n// Multipart of form fields and files\nresp, err := client.R().\n      SetFiles(map[string]string{\n        \"profile_img\": \"/Users/jeeva/test-img.png\",\n        \"notes\": \"/Users/jeeva/text-file.txt\",\n      }).\n      SetFormData(map[string]string{\n        \"first_name\": \"Jeevanandam\",\n        \"last_name\": \"M\",\n        \"zip_code\": \"00001\",\n        \"city\": \"my city\",\n        \"access_token\": \"C6A79608-782F-4ED0-A11D-BD82FAD829CD\",\n      }).\n      Post(\"http://myapp.com/profile\")\n```\n\n#### Sample Form submission\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// just mentioning about POST as an example with simple flow\n// User Login\nresp, err := client.R().\n      SetFormData(map[string]string{\n        \"username\": \"jeeva\",\n        \"password\": \"mypass\",\n      }).\n      Post(\"http://myapp.com/login\")\n\n// Followed by profile update\nresp, err := client.R().\n      SetFormData(map[string]string{\n        \"first_name\": \"Jeevanandam\",\n        \"last_name\": \"M\",\n        \"zip_code\": \"00001\",\n        \"city\": \"new city update\",\n      }).\n      Post(\"http://myapp.com/profile\")\n\n// Multi value form data\ncriteria := url.Values{\n  \"search_criteria\": []string{\"book\", \"glass\", \"pencil\"},\n}\nresp, err := client.R().\n      SetFormDataFromValues(criteria).\n      Post(\"http://myapp.com/search\")\n```\n\n#### Save HTTP Response into File\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Setting output directory path, If directory not exists then resty creates one!\n// This is optional one, if you're planning using absolute path in\n// `Request.SetOutput` and can used together.\nclient.SetOutputDirectory(\"/Users/jeeva/Downloads\")\n\n// HTTP response gets saved into file, similar to curl -o flag\n_, err := client.R().\n          SetOutput(\"plugin/ReplyWithHeader-v5.1-beta.zip\").\n          Get(\"http://bit.ly/1LouEKr\")\n\n// OR using absolute path\n// Note: output directory path is not used for absolute path\n_, err := client.R().\n          SetOutput(\"/MyDownloads/plugin/ReplyWithHeader-v5.1-beta.zip\").\n          Get(\"http://bit.ly/1LouEKr\")\n```\n\n#### Request URL Path Params\n\nResty 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.\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nclient.R().SetPathParams(map[string]string{\n   \"userId\": \"sample@sample.com\",\n   \"subAccountId\": \"100002\",\n}).\nGet(\"/v1/users/{userId}/{subAccountId}/details\")\n\n// Result:\n//   Composed URL - /v1/users/sample@sample.com/100002/details\n```\n\n#### Request and Response Middleware\n\nResty provides middleware ability to manipulate for Request and Response. It is more flexible than callback approach.\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Registering Request Middleware\nclient.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {\n    // Now you have access to Client and current Request object\n    // manipulate it as per your need\n\n    return nil  // if its success otherwise return error\n  })\n\n// Registering Response Middleware\nclient.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {\n    // Now you have access to Client and current Response object\n    // manipulate it as per your need\n\n    return nil  // if its success otherwise return error\n  })\n```\n\n#### OnError Hooks\n\nResty provides OnError hooks that may be called because:\n\n- The client failed to send the request due to connection timeout, TLS handshake failure, etc...\n- The request was retried the maximum amount of times, and still failed.\n\nIf there was a response from the server, the original error will be wrapped in `*resty.ResponseError` which contains the last response received.\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nclient.OnError(func(req *resty.Request, err error) {\n  if v, ok := err.(*resty.ResponseError); ok {\n    // v.Response contains the last response from the server\n    // v.Err contains the original error\n  }\n  // Log the error, increment a metric, etc...\n})\n```\n\n#### Generate CURL Command\n\u003eRefer: [curl_cmd_test.go](https://github.com/go-resty/resty/blob/v2/curl_cmd_test.go)\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nresp, err := client.R().\n    SetDebug(true).\n    EnableGenerateCurlOnDebug(). // CURL command generated when debug mode enabled with this option\n    SetBody(map[string]string{\"name\": \"Alex\"}).\n    Post(\"https://httpbin.org/post\")\n\ncurlCmdExecuted := resp.Request.GenerateCurlCommand()\n\n// Explore curl command\nfmt.Println(\"Curl Command:\\n  \", curlCmdExecuted+\"\\n\")\n\n/* Output\nCurl Command:\n   curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: go-resty/2.14.0 (https://github.com/go-resty/resty)' -d '{\"name\":\"Alex\"}' https://httpbin.org/post\n*/\n```\n\n#### Redirect Policy\n\nResty provides few ready to use redirect policy(s) also it supports multiple policies together.\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Assign Client Redirect Policy. Create one as per you need\nclient.SetRedirectPolicy(resty.FlexibleRedirectPolicy(15))\n\n// Wanna multiple policies such as redirect count, domain name check, etc\nclient.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20),\n                        resty.DomainCheckRedirectPolicy(\"host1.com\", \"host2.org\", \"host3.net\"))\n```\n\n##### Custom Redirect Policy\n\nImplement [RedirectPolicy](redirect.go#L20) interface and register it with resty client. Have a look [redirect.go](redirect.go) for more information.\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Using raw func into resty.SetRedirectPolicy\nclient.SetRedirectPolicy(resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {\n  // Implement your logic here\n\n  // return nil for continue redirect otherwise return error to stop/prevent redirect\n  return nil\n}))\n\n//---------------------------------------------------\n\n// Using struct create more flexible redirect policy\ntype CustomRedirectPolicy struct {\n  // variables goes here\n}\n\nfunc (c *CustomRedirectPolicy) Apply(req *http.Request, via []*http.Request) error {\n  // Implement your logic here\n\n  // return nil for continue redirect otherwise return error to stop/prevent redirect\n  return nil\n}\n\n// Registering in resty\nclient.SetRedirectPolicy(CustomRedirectPolicy{/* initialize variables */})\n```\n\n#### Custom Root Certificates and Client Certificates\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Custom Root certificates, just supply .pem file.\n// you can add one or more root certificates, its get appended\nclient.SetRootCertificate(\"/path/to/root/pemFile1.pem\")\nclient.SetRootCertificate(\"/path/to/root/pemFile2.pem\")\n// ... and so on!\n\n// Adding Client Certificates, you add one or more certificates\n// Sample for creating certificate object\n// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.\ncert1, err := tls.LoadX509KeyPair(\"certs/client.pem\", \"certs/client.key\")\nif err != nil {\n  log.Fatalf(\"ERROR client certificate: %s\", err)\n}\n// ...\n\n// You add one or more certificates\nclient.SetCertificates(cert1, cert2, cert3)\n```\n\n#### Custom Root Certificates and Client Certificates from string\n\n```go\n// Custom Root certificates from string\n// You can pass you certificates through env variables as strings\n// you can add one or more root certificates, its get appended\nclient.SetRootCertificateFromString(\"-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----\")\nclient.SetRootCertificateFromString(\"-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----\")\n// ... and so on!\n\n// Adding Client Certificates, you add one or more certificates\n// Sample for creating certificate object\n// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.\ncert1, err := tls.X509KeyPair([]byte(\"-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----\"), []byte(\"-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----\"))\nif err != nil {\n  log.Fatalf(\"ERROR client certificate: %s\", err)\n}\n// ...\n\n// You add one or more certificates\nclient.SetCertificates(cert1, cert2, cert3)\n```\n\n#### Proxy Settings\n\nDefault `Go` supports Proxy via environment variable `HTTP_PROXY`. Resty provides support via `SetProxy` \u0026 `RemoveProxy`.\nChoose as per your need.\n\n**Client Level Proxy** settings applied to all the request\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Setting a Proxy URL and Port\nclient.SetProxy(\"http://proxyserver:8888\")\n\n// Want to remove proxy setting\nclient.RemoveProxy()\n```\n\n#### Retries\n\nResty uses [backoff](http://www.awsarchitectureblog.com/2015/03/backoff.html)\nto increase retry intervals after each attempt.\n\nUsage example:\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Retries are configured per client\nclient.\n    // Set retry count to non zero to enable retries\n    SetRetryCount(3).\n    // You can override initial retry wait time.\n    // Default is 100 milliseconds.\n    SetRetryWaitTime(5 * time.Second).\n    // MaxWaitTime can be overridden as well.\n    // Default is 2 seconds.\n    SetRetryMaxWaitTime(20 * time.Second).\n    // SetRetryAfter sets callback to calculate wait time between retries.\n    // Default (nil) implies exponential backoff with jitter\n    SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {\n        return 0, errors.New(\"quota exceeded\")\n    })\n```\n\nBy default, resty will retry requests that return a non-nil error during execution.\nTherefore, the above setup will result in resty retrying requests with non-nil errors up to 3 times,\nwith the delay increasing after each attempt.\n\nYou can optionally provide client with [custom retry conditions](https://pkg.go.dev/github.com/go-resty/resty/v2#RetryConditionFunc):\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nclient.AddRetryCondition(\n    // RetryConditionFunc type is for retry condition function\n    // input: non-nil Response OR request execution error\n    func(r *resty.Response, err error) bool {\n        return r.StatusCode() == http.StatusTooManyRequests\n    },\n)\n```\n\nThe above example will make resty retry requests that end with a `429 Too Many Requests` status code.\nIt's important to note that when you specify conditions using `AddRetryCondition`,\nit will override the default retry behavior, which retries on errors encountered during the request.\nIf you want to retry on errors encountered during the request, similar to the default behavior,\nyou'll need to configure it as follows:\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\nclient.AddRetryCondition(\n    func(r *resty.Response, err error) bool {\n        // Including \"err != nil\" emulates the default retry behavior for errors encountered during the request.\n        return err != nil || r.StatusCode() == http.StatusTooManyRequests\n    },\n)\n```\n\nMultiple retry conditions can be added.\nNote that if multiple conditions are specified, a retry will occur if any of the conditions are met.\n\nIt is also possible to use `resty.Backoff(...)` to get arbitrary retry scenarios\nimplemented. [Reference](retry_test.go).\n\n#### Allow GET request with Payload\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Allow GET request with Payload. This is disabled by default.\nclient.SetAllowGetMethodPayload(true)\n```\n\n#### Wanna Multiple Clients\n\n```go\n// Here you go!\n// Client 1\nclient1 := resty.New()\nclient1.R().Get(\"http://httpbin.org\")\n// ...\n\n// Client 2\nclient2 := resty.New()\nclient2.R().Head(\"http://httpbin.org\")\n// ...\n\n// Bend it as per your need!!!\n```\n\n#### Remaining Client Settings \u0026 its Options\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Unique settings at Client level\n//--------------------------------\n// Enable debug mode\nclient.SetDebug(true)\n\n// Assign Client TLSClientConfig\n// One can set custom root-certificate. Refer: http://golang.org/pkg/crypto/tls/#example_Dial\nclient.SetTLSClientConfig(\u0026tls.Config{ RootCAs: roots })\n\n// or One can disable security check (https)\nclient.SetTLSClientConfig(\u0026tls.Config{ InsecureSkipVerify: true })\n\n// Set client timeout as per your need\nclient.SetTimeout(1 * time.Minute)\n\n\n// You can override all below settings and options at request level if you want to\n//--------------------------------------------------------------------------------\n// Host URL for all request. So you can use relative URL in the request\nclient.SetBaseURL(\"http://httpbin.org\")\n\n// Headers for all request\nclient.SetHeader(\"Accept\", \"application/json\")\nclient.SetHeaders(map[string]string{\n        \"Content-Type\": \"application/json\",\n        \"User-Agent\": \"My custom User Agent String\",\n      })\n\n// Cookies for all request\nclient.SetCookie(\u0026http.Cookie{\n      Name:\"go-resty\",\n      Value:\"This is cookie value\",\n      Path: \"/\",\n      Domain: \"sample.com\",\n      MaxAge: 36000,\n      HttpOnly: true,\n      Secure: false,\n    })\nclient.SetCookies(cookies)\n\n// URL query parameters for all request\nclient.SetQueryParam(\"user_id\", \"00001\")\nclient.SetQueryParams(map[string]string{ // sample of those who use this manner\n      \"api_key\": \"api-key-here\",\n      \"api_secret\": \"api-secret\",\n    })\nclient.R().SetQueryString(\"productId=232\u0026template=fresh-sample\u0026cat=resty\u0026source=google\u0026kw=buy a lot more\")\n\n// Form data for all request. Typically used with POST and PUT\nclient.SetFormData(map[string]string{\n    \"access_token\": \"BC594900-518B-4F7E-AC75-BD37F019E08F\",\n  })\n\n// Basic Auth for all request\nclient.SetBasicAuth(\"myuser\", \"mypass\")\n\n// Bearer Auth Token for all request\nclient.SetAuthToken(\"BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F\")\n\n// Enabling Content length value for all request\nclient.SetContentLength(true)\n\n// Registering global Error object structure for JSON/XML request\nclient.SetError(\u0026Error{})    // or resty.SetError(Error{})\n```\n\n#### Unix Socket\n\n```go\nunixSocket := \"/var/run/my_socket.sock\"\n\n// Create a Go's http.Transport so we can set it in resty.\ntransport := http.Transport{\n\tDial: func(_, _ string) (net.Conn, error) {\n\t\treturn net.Dial(\"unix\", unixSocket)\n\t},\n}\n\n// Create a Resty Client\nclient := resty.New()\n\n// Set the previous transport that we created, set the scheme of the communication to the\n// socket and set the unixSocket as the HostURL.\nclient.SetTransport(\u0026transport).SetScheme(\"http\").SetBaseURL(unixSocket)\n\n// No need to write the host's URL on the request, just the path.\nclient.R().Get(\"http://localhost/index.html\")\n```\n\n#### Bazel Support\n\nResty can be built, tested and depended upon via [Bazel](https://bazel.build).\nFor example, to run all tests:\n\n```shell\nbazel test :resty_test\n```\n\n#### Mocking http requests using [httpmock](https://github.com/jarcoal/httpmock) library\n\nIn order to mock the http requests when testing your application you\ncould use the `httpmock` library.\n\nWhen using the default resty client, you should pass the client to the library as follow:\n\n```go\n// Create a Resty Client\nclient := resty.New()\n\n// Get the underlying HTTP Client and set it to Mock\nhttpmock.ActivateNonDefault(client.GetClient())\n```\n\nMore detailed example of mocking resty http requests using ginko could be found [here](https://github.com/jarcoal/httpmock#ginkgo--resty-example).\n\n## Versioning\n\nResty releases versions according to [Semantic Versioning](http://semver.org)\n\n  * Resty v2 does not use `gopkg.in` service for library versioning.\n  * Resty fully adapted to `go mod` capabilities since `v1.10.0` release.\n  * 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`.\n  * 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.\n\n## Contribution\n\nI 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.\n\nBTW, 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.\n\n## Creator\n\n[Jeevanandam M.](https://github.com/jeevatkm) (jeeva@myjeeva.com)\n\n## Core Team\n\nHave a look on [Members](https://github.com/orgs/go-resty/people) page.\n\n## Contributors\n\nHave a look on [Contributors](https://github.com/go-resty/resty/graphs/contributors) page.\n\n## License\n\nResty released under MIT license, refer [LICENSE](LICENSE) file.\n","funding_links":[],"categories":["实用工具","實用工具"],"sub_categories":["高级控制台界面","高級控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-resty%2Fresty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGo-resty%2Fresty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-resty%2Fresty/lists"}