Skip to content

Commit

Permalink
bug #32689 [HttpClient] rewind stream when using Psr18Client (nicolas…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
fabpot committed Jul 24, 2019
2 parents 280fd7d + 7f4362b commit 1fc080b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/HttpClient/Psr18Client.php
Expand Up @@ -65,9 +65,15 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
public function sendRequest(RequestInterface $request): ResponseInterface
{
try {
$body = $request->getBody();

if ($body->isSeekable()) {
$body->seek(0);
}

$response = $this->client->request($request->getMethod(), (string) $request->getUri(), [
'headers' => $request->getHeaders(),
'body' => (string) $request->getBody(),
'body' => $body->getContents(),
'http_version' => '1.0' === $request->getProtocolVersion() ? '1.0' : null,
]);

Expand All @@ -79,7 +85,13 @@ public function sendRequest(RequestInterface $request): ResponseInterface
}
}

return $psrResponse->withBody($this->streamFactory->createStream($response->getContent(false)));
$body = $this->streamFactory->createStream($response->getContent(false));

if ($body->isSeekable()) {
$body->seek(0);
}

return $psrResponse->withBody($body);
} catch (TransportExceptionInterface $e) {
if ($e instanceof \InvalidArgumentException) {
throw new Psr18RequestException($e, $request);
Expand Down

0 comments on commit 1fc080b

Please sign in to comment.