Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fc080b

Browse files
committed
bug #32689 [HttpClient] rewind stream when using Psr18Client (nicolas-grekas)
This PR was merged into the 4.3 branch. Discussion ---------- [HttpClient] rewind stream when using Psr18Client | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony-docs#11996 | License | MIT | Doc PR | - This is not a bug fix technically but just how PSR-7 works. I'm glad we did not make it a first-class thing in Symfony. This makes it a bit more practicable if it can be... Commits ------- 7f4362b [HttpClient] rewind stream when using Psr18Client
2 parents 280fd7d + 7f4362b commit 1fc080b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Symfony/Component/HttpClient/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)