Skip to content

Commit 3847741

Browse files
author
Kyra Farrow
committed
[HttpClient] rewind stream when using Psr18Client
1 parent 28b9ac7 commit 3847741

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Psr18Client.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
6565
public function sendRequest(RequestInterface $request): ResponseInterface
6666
{
6767
try {
68+
$body = $request->getBody();
69+
70+
if ($body->isSeekable()) {
71+
$body->seek(0);
72+
}
73+
6874
$response = $this->client->request($request->getMethod(), (string) $request->getUri(), [
6975
'headers' => $request->getHeaders(),
70-
'body' => (string) $request->getBody(),
76+
'body' => $body->getContents(),
7177
'http_version' => '1.0' === $request->getProtocolVersion() ? '1.0' : null,
7278
]);
7379

@@ -79,7 +85,13 @@ public function sendRequest(RequestInterface $request): ResponseInterface
7985
}
8086
}
8187

82-
return $psrResponse->withBody($this->streamFactory->createStream($response->getContent(false)));
88+
$body = $this->streamFactory->createStream($response->getContent(false));
89+
90+
if ($body->isSeekable()) {
91+
$body->seek(0);
92+
}
93+
94+
return $psrResponse->withBody($body);
8395
} catch (TransportExceptionInterface $e) {
8496
if ($e instanceof \InvalidArgumentException) {
8597
throw new Psr18RequestException($e, $request);

0 commit comments

Comments
 (0)