Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using dontTruncateRequestExceptions() results in an empty response body #54207

Open
dylanbr opened this issue Jan 15, 2025 · 2 comments
Open

Comments

@dylanbr
Copy link

dylanbr commented Jan 15, 2025

Laravel Version

11.37.0

PHP Version

8.3.15

Database Driver & Version

n/a

Description

By default, truncateRequestExceptionsAt() is used, which causes RequestException to call Message::bodySummary(). This checks if the body isSeekable(), and then uses rewind() on the body after extracting the summary. The result is that, after catching the exception, $e->response->getBody()->getContents() will return the the contents of the body.

However, when dontTruncateRequestExceptions() is used, RequestException calls Message::toString() instead. This instead casts getBody() to a string to get the full contents of the body. The result is that, after catching the exception, $e->response->getBody()->getContents() will return an empty string.

Now I'm not sure if this is something that should be fixed upstream in Guzzle, or left as-is and documented somewhere, but I think it makes sense to report it here first.

Steps To Reproduce

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

// The same as calling `truncateRequestExceptionsAt()`
RequestException::truncateAt(120);
try {
    Http::get('https://laravel.com/invalid-file')->throw();
} catch (RequestException $e) {
    // This will output the "Not found" HTML page from the Laravel website.
    dump($e->response->getBody()->getContents());
}

// The same as calling `dontTruncateRequestExceptions()`
RequestException::dontTruncate();
try {
    Http::get('https://laravel.com/invalid-file')->throw();
} catch (RequestException $e) {
    // This will output an empty string.
    dump($e->response->getBody()->getContents());
}
@dylanbr
Copy link
Author

dylanbr commented Jan 15, 2025

This is less of a problem, and may be intentional, but I'd like to note it here too in a comment.

When using truncateRequestExceptionsAt() the exception message contains only the status code and response body, e.g.:

"HTTP request returned status code 418:\n{\"error\":\"I'm a teapot\"}\n"

When using dontTruncateRequestExceptions(), the exception message also contains the headers, e.g.:

"HTTP request returned status code 418:\nHTTP/1.1 418 I'm a teapot\r\nContent-Type: application/json\r\n\r\n{\"error\":\"I'm a teapot\"}\n"

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants