Search code examples
phpsymfonyhttpguzzle6

Symfony 2 - Guzzle 6.X HTTP | get the body response


I am using Guzzle with Symfony 2.7.3 and I don't know why I have the header of the response but not the body. (I am on localhost with WAMP)

$donnees = array(// Base URI is used with relative requests
            'base_uri' => $urlAuth,
            // You can set any number of default request options.
            'timeout'  => 2.0,
            'headers' => [
                'User-Agent' => 'testing/1.0',
                'Accept'     => 'application/json'
            ],
            'verify' => false,
            'json'      => ["Id" => $Id, 
                                                "Username" => $username,
                                                "Password" => $password,
                                                "SecretId" => $secretId]
            );

        $client = new Client($donnees);

        $response = $client->post( '/auth/', $donnees );
dump($response);

so I got :

enter image description here

But stream is empty whereas I should get a token (you can see content-length : 69)

Can you help me, I don't know that I missed...

(The server only accept POST to get the token)


Solution

  • As it's json response you should decode it, add:

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

    true means that returned objects will be converted into associative arrays.