Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http client psr 18 example not working #11996

Closed
LPodolski opened this issue Jul 20, 2019 · 1 comment
Closed

http client psr 18 example not working #11996

LPodolski opened this issue Jul 20, 2019 · 1 comment

Comments

@LPodolski
Copy link

https://github.com/symfony/symfony-docs/blob/4.3/components/http_client.rst#psr-18

on full symfony project from symfony new --full project_name and after installing requirements

        $psr17Factory = new Psr17Factory();
        $psr18Client = new Psr18Client();

        $url = 'https://symfony.com/versions.json';
        $request = $psr17Factory->createRequest('GET', $url);
        $response = $psr18Client->sendRequest($request);

//        $response->getBody()->rewind(); // <--------------------- this line changed

        $content = json_decode($response->getBody()->getContents(), true);




        return new Response(\print_r($content, true));

does not work, returns empty content

if I uncomment line (added by me, not present in docs):

        $response->getBody()->rewind(); // <--------------------- this line changed

it also works

and If i switch http driver to guzzle it also works:

<?php

declare(strict_types=1);

namespace App\Controller;

use Http\Adapter\Guzzle6\Client;
use Http\Factory\Guzzle\RequestFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController extends AbstractController
{
    /**
     * @Route("/test", name="app_test")
     */
    public function index(): Response
    {
        $psr17Factory = new RequestFactory();
        $psr18Client = Client::createWithConfig([]);

        $url = 'https://symfony.com/versions.json';
        $request = $psr17Factory->createRequest('GET', $url);
        $response = $psr18Client->sendRequest($request);

        $content = json_decode($response->getBody()->getContents(), true);




        return new Response(\print_r($content, true));
    }
}

so it is problem with symfony's http client,
most likely there is some issue with rewind of stream before returning contents

fabpot added a commit to symfony/symfony that referenced this issue Jul 24, 2019
…-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
symfony-splitter pushed a commit to symfony/http-client that referenced this issue Jul 24, 2019
…-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
-------

7f4362bd46 [HttpClient] rewind stream when using Psr18Client
@nicolas-grekas
Copy link
Member

This will be fixed at the code level once symfony/symfony#32689 is tagged (in one week max)
Meanwhile, you can use __toString instead of getContents.
Thanks for the report.

sadafrangian3 pushed a commit to sadafrangian3/Dependency-Injection-http-client that referenced this issue Nov 2, 2022
…-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
-------

7f4362bd46 [HttpClient] rewind stream when using Psr18Client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants