How do I add request/response logging with resty in Go?

To add request and response logging with Resty in Go, you can utilize the built-in middleware provided by the Resty client. This allows you to log the details of each HTTP request and response, making it easier to debug and monitor your API interactions.

Example of Request/Response Logging with Resty

package main import ( "fmt" "github.com/go-resty/resty/v2" "log" ) func main() { // Create a Resty client client := resty.New() // Set up logging middleware client.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { log.Printf("Request: %s %s", r.Method, r.URL) return nil }) client.OnAfterResponse(func(c *resty.Client, r *resty.Response) error { log.Printf("Response: %d %s", r.StatusCode(), r.Body()) return nil }) // Make a GET request resp, err := client.R().Get("https://jsonplaceholder.typicode.com/todos/1") if err != nil { log.Fatalf("Error occurred: %v", err) } fmt.Println("Response Body:", resp.String()) }

Resty Golang HTTP logging API client request logging response logging