Skip to content

Preserve request body in debug output for all methods#751

Open
bkiran6398 wants to merge 2 commits intomainfrom
fix_api_log
Open

Preserve request body in debug output for all methods#751
bkiran6398 wants to merge 2 commits intomainfrom
fix_api_log

Conversation

@bkiran6398
Copy link
Copy Markdown

🔧 Changes

Fixes DebugTransport to preserve request bodies in debug log output for POST, PATCH, and PUT requests.

Previously, DebugTransport called dumpRequest after base.RoundTrip() to capture headers set by inner transports. However, RoundTrip consumes the request body stream, so httputil.DumpRequestOut found an empty body — making debug logs useless for any request with a payload.

  • Buffers the request body before RoundTrip and restores it afterward for dumping
  • GET requests and error paths continue to work correctly

📚 References

🔬 Testing

Added 5 new unit tests to TestDebugTransport:

  • POST body preserved — mock transport consumes body, asserts body appears in log
  • PATCH body preserved — same pattern for PATCH method
  • PUT body preserved — same pattern for PUT method
  • Body preserved on error — verifies body is logged even when transport returns an error
  • GET with no body — confirms nil-body path is unaffected

Run tests:

go test ./internal/client/ -run TestDebugTransport -v

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

- Enhanced the DebugTransport function to save and restore the request body
  for POST, PATCH, and PUT requests, ensuring the body is available for logging
  even after being consumed by the transport.
- Added tests to verify that the request body is correctly logged in debug output
  for various HTTP methods, including error scenarios.
- This change improves the debugging capabilities of the SDK by providing
  complete visibility into the requests being made.
@bkiran6398 bkiran6398 requested a review from a team as a code owner April 9, 2026 12:34
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.25%. Comparing base (ceabda7) to head (3bae315).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #751   +/-   ##
=======================================
  Coverage   87.25%   87.25%           
=======================================
  Files         350      350           
  Lines      139272   139278    +6     
=======================================
+ Hits       121519   121525    +6     
  Misses      13219    13219           
  Partials     4534     4534           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@developerkunal developerkunal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small suggestions for the body-buffering block:

  1. The original req.Body is never explicitly closed after ReadAll. It would be good to call req.Body.Close() before replacing it.

  2. The error from io.ReadAll is silently discarded. If reading fails partway through, a truncated body would be sent to the server without the caller knowing. In practice this is unlikely since bodies are typically bytes.Reader or strings.Reader, but it's still worth handling.

Something like:

var bodyBytes []byte
if req.Body != nil {
    bodyBytes, err := io.ReadAll(req.Body)
    req.Body.Close()
    if err != nil {
        return nil, fmt.Errorf("debug transport: failed to read request body: %w", err)
    }
    req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}

Otherwise the fix and tests look good!

- Cleaned up the DebugTransport function by removing an empty line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants