Skip to content

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

@dylanbr

Description

@dylanbr

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());
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions