From 5705431b4cf6d4dbff28adfd25040a7f93f54ea8 Mon Sep 17 00:00:00 2001 From: Tom Harper Date: Fri, 29 Sep 2023 16:39:16 +0100 Subject: [PATCH] Change how we fetch the initial contents to avoid having to rewind in getter (#11) --- src/Http/Response.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 08ab55e..e5651c7 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -63,7 +63,13 @@ public function __construct( $this->response = $response; $this->paginationHandler = $paginationHandler; - $this->contents = $this->response->getBody()->getContents(); + $this->contents = (string) $this->response->getBody(); + + /** + * Now we've fetched the contents, rewind the stream to ensure anyone fetching the PSR-7 response + * can read the body correctly without having to rewind first. + */ + $this->response->getBody()->rewind(); } public function toPsr7Request(): RequestInterface @@ -73,9 +79,6 @@ public function toPsr7Request(): RequestInterface public function toPsr7Response(): ResponseInterface { - // Ensure the body is rewound so the consumer can read the contents of the body if they want to. - $this->response->getBody()->rewind(); - return $this->response; }