Request and Response Middleware

Resty provides the ability to use middleware to intercept requests before and responses after, allowing for custom processing of request and response operations. This approach is more flexible than callback functions.

// Create a Resty client
client := resty.New()

// Register request middleware
client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
    // Now you can access the client and the current request object
    // Perform operations as needed

    return nil  // Return nil if successful, otherwise return an error
  })

// Register response middleware
client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
    // Now you can access the client and the current response object
    // Perform operations as needed

    return nil  // Return nil if successful, otherwise return an error
  })

Error Hooks

Resty provides an OnError hook, which may be called in the following cases:

  • The client is unable to send a request due to connection timeout, TLS handshake failure, etc.
  • The request has been retried the maximum number of times but still fails.

If the server responds, the original error will be wrapped in *resty.ResponseError, which contains the last received response.

// Create a Resty client
client := resty.New()

client.OnError(func(req *resty.Request, err error) {
  if v, ok := err.(*resty.ResponseError); ok {
    // v.Response contains the server's last response
    // v.Err contains the original error
  }
  // Log errors, increase metrics, etc.
})

Redirection Policies

Resty provides several available redirection policies and supports the simultaneous use of multiple policies.

// Create a Resty client
client := resty.New()

// Set the redirection policy for the client. Create a policy as needed
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(15))

// Use multiple policies, such as redirection count, domain check, etc.
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20),
                        resty.DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))

Custom Redirection Policy

Implement the RedirectPolicy interface and register it with the resty client. Please refer to redirect.go for more information.

// Create a Resty client
client := resty.New()

// Use the original function with resty.SetRedirectPolicy
client.SetRedirectPolicy(resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
  // Implement your logic here

  // Return nil to continue the redirection, or return an error to stop/prevent redirection
  return nil
}))

//---------------------------------------------------

// Create a more flexible redirection policy using a structure
type CustomRedirectPolicy struct {
  // Define variables here
}

func (c *CustomRedirectPolicy) Apply(req *http.Request, via []*http.Request) error {
  // Implement your logic here

  // Return nil to continue the redirection, or return an error to stop/prevent redirection
  return nil
}

// Register in resty
client.SetRedirectPolicy(CustomRedirectPolicy{/* Initialize variables */})

Proxy Settings

By default, Go supports proxies through the environment variable HTTP_PROXY. Resty provides proxy support through SetProxy and RemoveProxy. Choose according to your needs.

Proxy settings at the client level will be applied to all requests.

// Create a Resty client
client := resty.New()

// Set the proxy URL and port
client.SetProxy("http://proxyserver:8888")

// To remove the proxy settings
client.RemoveProxy()