From a080e3b5451983d592fbc887328237f369731348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Verry?= Date: Sat, 25 Mar 2023 13:09:40 +0100 Subject: [PATCH] Fix: sending JSON or multipart requests Sending url-encoded params, json or multipart requests with Guzzle is easy, because shortcuts are implemented within [`GuzzleHttp\Client::applyOptions()`](https://github.com/guzzle/guzzle/blob/7.5.0/src/Client.php#L340). Currently, those shortcuts are unreachable through the League OAuth2 Client, and thus all its providers. This fix relies on `AbstractProvider::getResponse()`, and does not affect `AbstractProvider::getAuthenticatedRequest()`. `GuzzleHttp\ClientInterface::send()` method supports a second optional `$options` parameter [since v6.0](https://github.com/guzzle/guzzle/commit/1a9ad6b55368283aaf73672a519e5177530991fb?diff=split#diff-7fe9d7bab1528c58fb39365621c6f02430dd01828c26fe8b00f0408de72c71b8). Upgrading `AbstractProvider::getResponse()` to support this second optional parameter allows the developper to use a `"form_params"`, `"json"` or `"multipart"` option key : ```php $options = ['multipart' => $multipart]; $request = $oauth2client->getAuthenticatedRequest($method, $url, $token); $response = $oauth2client->getResponse($request, $options); ``` --- src/Provider/AbstractProvider.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index a6c7a8fd..008385de 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -698,25 +698,29 @@ protected function createRequest($method, $url, $token, array $options) * WARNING: This method does not attempt to catch exceptions caused by HTTP * errors! It is recommended to wrap this method in a try/catch block. * - * @param RequestInterface $request + * @param RequestInterface $request Request to send + * @param array $options Request options to apply to the given + * request and to the transfer. * @return ResponseInterface */ - public function getResponse(RequestInterface $request) + public function getResponse(RequestInterface $request, array $options = []) { - return $this->getHttpClient()->send($request); + return $this->getHttpClient()->send($request, $options); } /** * Sends a request and returns the parsed response. * - * @param RequestInterface $request + * @param RequestInterface $request Request to send + * @param array $options Request options to apply to the given + * request and to the transfer. * @throws IdentityProviderException * @return mixed */ - public function getParsedResponse(RequestInterface $request) + public function getParsedResponse(RequestInterface $request, array $options = []) { try { - $response = $this->getResponse($request); + $response = $this->getResponse($request, $options); } catch (BadResponseException $e) { $response = $e->getResponse(); }