Skip to content

Preserve request body in debug output for all methods#751

Open
bkiran6398 wants to merge 4 commits intomainfrom
fix_api_log
Open

Preserve request body in debug output for all methods#751
bkiran6398 wants to merge 4 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)

@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

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.28%. Comparing base (141f2a7) to head (0330ff0).

Files with missing lines Patch % Lines
internal/client/client.go 80.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #751      +/-   ##
==========================================
+ Coverage   85.23%   87.28%   +2.05%     
==========================================
  Files         350      350              
  Lines      161461   140171   -21290     
==========================================
- Hits       137615   122347   -15268     
+ Misses      19288    13265    -6023     
- Partials     4558     4559       +1     

☔ 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!

- 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.
- Cleaned up the DebugTransport function by removing an empty line.
- Added error handling for reading the request body in the DebugTransport function.
- Ensured that the request body is closed after reading to prevent resource leaks.
- Improved robustness by returning an error message if reading the body fails.
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