Request Inspection
Similar to using curl -v
, it can often be useful to inspect the raw request and response when debugging or understanding the behavior of a server.
the httpr.Inspect
option can be used to log the request and response to stdout. This option can be set globally when creating a client or per request
Global
httpc := httpr.NewClient( httpr.Inspect(),)
resp, err := httpc.Get(context.Background(), "https://jsonplaceholder.typicode.com/posts/1")
Per Request
httpc := httpr.NewClient()
resp, err := httpc.Get(context.Background(), "https://jsonplaceholder.typicode.com/posts/1", httpr.Inspect(),)
Output
inspecting a GET
request to https://jsonplaceholder.typicode.com/posts/1
will produce the following output:
Request:GET /posts/1 HTTP/1.1Host: jsonplaceholder.typicode.comUser-Agent: Go-http-client/1.1Accept-Encoding: gzip
Response:HTTP/2.0 200 OKAccess-Control-Allow-Credentials: trueAge: 1325Alt-Svc: h3=":443"; ma=86400Cache-Control: max-age=43200Cf-Cache-Status: HITCf-Ray: 8cca10deeae216d0-IAHContent-Type: application/json; charset=utf-8Date: Thu, 03 Oct 2024 04:07:32 GMTEtag: W/"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU"Expires: -1Nel: {"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}Pragma: no-cacheReport-To: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1727095511&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=NGGjjBYeTfBRqq5CLZIxTDJ3FZ8%2F95hmFn9b1UR4xQ4%3D"}]}Reporting-Endpoints: heroku-nel=https://nel.heroku.com/reports?ts=1727095511&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=NGGjjBYeTfBRqq5CLZIxTDJ3FZ8%2F95hmFn9b1UR4xQ4%3DServer: cloudflareVary: Origin, Accept-EncodingVia: 1.1 vegurX-Content-Type-Options: nosniffX-Powered-By: ExpressX-Ratelimit-Limit: 1000X-Ratelimit-Remaining: 999X-Ratelimit-Reset: 1727095568
{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}