guzzle-cache-middleware icon indicating copy to clipboard operation
guzzle-cache-middleware copied to clipboard

Response object without body?

Open piotrmerton opened this issue 3 years ago • 2 comments

Hello, I am having hard time trying to grasp the logic of accessing cached data. Can you point me in the right direction what to do after file gets created? The file is created fine with data I'm interested in but using $response->getBody() returns nothing? How can I access data?

Example I'm using:

`

$stack = HandlerStack::create();
$stack->push(
    new CacheMiddleware(
        new GreedyCacheStrategy(
            new FlysystemStorage(
                new Local($cache_dir)
            ),
            $ttl
        )
    ), 
    "greedy-cache"
);
$client = new Client(['handler' => $stack]);
$response->getBody() //this is empty

` This is the response object I'm getting (the file is almost the same, though serialized, and with response body):

`

(
    [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
    [statusCode:GuzzleHttp\Psr7\Response:private] => 200
    [headers:GuzzleHttp\Psr7\Response:private] => Array
        (
            [Server] => Array
                (
                    [0] => openresty
                )

            [Date] => Array
                (
                    [0] => Tue, 25 Jan 2022 22:17:14 GMT
                )

            [Content-Type] => Array
                (
                    [0] => application/json
                )

            [Transfer-Encoding] => Array
                (
                    [0] => chunked
                )

            [Connection] => Array
                (
                    [0] => keep-alive
                )

            [X-Request-ID] => Array
                (
                    [0] => f35b0bda-a778-48e7-8372-406d66121536
                    [1] => f35b0bda-a778-48e7-8372-406d66121536
                )

            [Strict-Transport-Security] => Array
                (
                    [0] => max-age=31536000
                )

            [Vary] => Array
                (
                    [0] => Accept-Encoding
                    [1] => Accept-Encoding
                )

            [Access-Control-Allow-Origin] => Array
                (
                    [0] => *
                )

            [Access-Control-Allow-Credentials] => Array
                (
                    [0] => True
                )

            [Access-Control-Allow-Methods] => Array
                (
                    [0] => GET, OPTIONS
                )

            [Access-Control-Allow-Headers] => Array
                (
                    [0] => x-rapidapi-key, x-apisports-key, x-rapidapi-host
                )

            [X-RateLimit-Limit] => Array
                (
                    [0] => 10
                )

            [X-RateLimit-Remaining] => Array
                (
                    [0] => 9
                )

            [x-ratelimit-requests-limit] => Array
                (
                    [0] => 100
                )

            [x-ratelimit-requests-remaining] => Array
                (
                    [0] => 57
                )

            [Warning] => Array
                (
                    [0] => 299 - "Cached although the response headers indicate not to do it!" "Tue, 25 Jan 2022 22:17:14 +0000"
                )

            [Age] => Array
                (
                    [0] => 4246
                )

            [X-Kevinrob-Cache] => Array
                (
                    [0] => HIT
                )

        )

    [headerNames:GuzzleHttp\Psr7\Response:private] => Array
        (
            [server] => Server
            [date] => Date
            [content-type] => Content-Type
            [transfer-encoding] => Transfer-Encoding
            [connection] => Connection
            [x-request-id] => X-Request-ID
            [strict-transport-security] => Strict-Transport-Security
            [vary] => Vary
            [access-control-allow-origin] => Access-Control-Allow-Origin
            [access-control-allow-credentials] => Access-Control-Allow-Credentials
            [access-control-allow-methods] => Access-Control-Allow-Methods
            [access-control-allow-headers] => Access-Control-Allow-Headers
            [x-ratelimit-limit] => X-RateLimit-Limit
            [x-ratelimit-remaining] => X-RateLimit-Remaining
            [x-ratelimit-requests-limit] => x-ratelimit-requests-limit
            [x-ratelimit-requests-remaining] => x-ratelimit-requests-remaining
            [x-kevinrob-cache] => X-Kevinrob-Cache
            [warning] => Warning
            [age] => Age
        )

    [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
    [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #6
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )

        )

)

` Any help how to proceed? Thanks.

piotrmerton avatar Jan 25 '22 23:01 piotrmerton

Have you tried $response->getBody()->getContents()?

rhysemmerson avatar Jan 31 '22 23:01 rhysemmerson

Have you tried $response->getBody()->getContents()?

Thank you, it worked 👍

piotrmerton avatar Feb 01 '22 15:02 piotrmerton