Skip to content

Fix for empty response body when debugging unseekable streams#537

Merged
Sammyjo20 merged 3 commits intosaloonphp:v4from
JonPurvis:fix/447-debug-empty-body
Mar 25, 2026
Merged

Fix for empty response body when debugging unseekable streams#537
Sammyjo20 merged 3 commits intosaloonphp:v4from
JonPurvis:fix/447-debug-empty-body

Conversation

@JonPurvis
Copy link
Copy Markdown
Contributor

Fixes #447

When debug mode is on, the default response debugger calls $response->body() to dump the response. If the PSR-7 body stream is unseekable (e.g. many LLM/OpenAI APIs), that first read consumes the stream and it is never rewound, so later calls to body() or json() return empty.

This change:

  1. Response::withBufferedBody(string $body): static
    Returns a new Response with the same metadata but the body replaced by a seekable stream containing the given string (using ResponseInterface::withBody() and GuzzleHttp\Psr7\Utils::streamFor()).

  2. HasDebugging::debugResponse()
    In the response middleware: if the PSR body stream is not seekable, we read the body once, build a replacement response via withBufferedBody(), run the debug callback on that response (so the dump still shows the full body), and return the replacement response from the middleware so the rest of the pipeline and the caller receive a response whose body can still be read. If the stream is seekable, behavior is unchanged (same response, no buffering).

@JonPurvis JonPurvis changed the base branch from v3 to v4 March 25, 2026 20:53
@Sammyjo20 Sammyjo20 changed the title fix for empty response body when debugging unseekable streams Fix for empty response body when debugging unseekable streams Mar 25, 2026
@Sammyjo20 Sammyjo20 merged commit 96cedff into saloonphp:v4 Mar 25, 2026
18 checks passed
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.

Debugging middleware results in empty body

2 participants