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

Not compatible with gmponos/guzzle_logger #166

Open
compwright opened this issue Aug 23, 2022 · 2 comments
Open

Not compatible with gmponos/guzzle_logger #166

compwright opened this issue Aug 23, 2022 · 2 comments

Comments

@compwright
Copy link

Since CacheEntry uses PumpStream for the response, attempting to use this middleware with gmponos/guzzle-log-middleware results in the following error:

StringHandler can not log request/response because the body is not seekable/readable.

@compwright
Copy link
Author

Workaround:

use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\ResponseInterface;

// Without this, we get the following error from the logger middleware:
//
// "StringHandler can not log request/response because the body is not seekable/readable"
//
// It happens because the cache handler returns the body in the form of PumpStream which
// is not rewindable. To fix, we simply read and copy the stream to a rewindable stream.
//
$handler->push(Middleware::mapResponse(
    fn (ResponseInterface $response) => $response->withBody(
        Psr7Utils::streamFor((string) $response->getBody())
    )
));

// Add the cache middleware
$handler->push($cacheMiddleware, 'cache');

@intel352
Copy link

I'm using the cache middleware w/ gmponos without error. I'm using Monolog rather than StringHandler, so perhaps it's StringHandler specifically that isn't compatible.

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

No branches or pull requests

2 participants