Skip to content

http client psr 18 example not working #11996

Closed
@LPodolski

Description

@LPodolski

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions