8p / EightPointsGuzzleBundle

⛽️ Integrates Guzzle 6.x, a PHP HTTP Client, into Symfony
MIT License
440 stars 71 forks source link

No matching accepted Response format could be determined #81

Closed numediaweb closed 7 years ago

numediaweb commented 7 years ago

When I call an oAuth v2 end point with CURL it works:

$handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($handle, CURLOPT_POST, count($parameters));
        curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($parameters));
        $result = curl_exec($handle);

        curl_close($handle);

        return json_decode($result, true);

When I use the Guzzle client:

try {
            $headers = [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ];
            $options = [
                'form_params' => [
                    'grant_type' => 'client_credentials',
                    'client_id' => $this->connectClientId,
                    'client_secret' => $this->connectClientSecret,
                ],
                'set_headers' => $headers,
            ];
            $response = $this->client->post('/oauth/v2/token', $options);
        } catch (BadResponseException $e) {
            $response = $e->getResponse();
            $body = $response->getBody();
            die($body->getContents());
        }

I get this error (from the end point which is also a Symfony application):

No matching accepted Response format could be determined
in vendor/friendsofsymfony/rest-bundle/EventListener/FormatListener.php at line 76

Why does it work in CURL and not in Guzzle? I'am using "eightpoints/guzzle-bundle": "^5.2", with this config https://github.com/8p/GuzzleBundle/issues/77

florianpreusner commented 7 years ago

Why are you using 'set_headers' instead of 'headers' as a key in $options? http://docs.guzzlephp.org/en/latest/request-options.html#headers

florianpreusner commented 7 years ago

Maybe the documentation is not really clear: (@deprecated, will be removed in v6; new: set headers in options) I will change that sentence if this was the issue.

numediaweb commented 7 years ago

You are right @florianpreusner the description might be confusing. I fixed it now by setting the correct header:

try {
    $options = [
        'headers' => [
            'Accept' => ['text/html']
        ],
        'form_params' => [
            'grant_type' => 'client_credentials',
            'client_id' => $this->connectClientId,
            'client_secret' => $this->connectClientSecret,
        ],
    ];
    $response = $this->client->post('/oauth/v2/token?XDEBUG_SESSION_START=19341', $options);
} catch (BadResponseException $e) {
}
dump(json_decode($response->getBody(), true));

This issue can be closed :)

florianpreusner commented 7 years ago

👍

Fixed description: https://github.com/8p/GuzzleBundle/commit/2b6eb8ad5169d573e701075fe90118deb6e076b5