oauth2-client icon indicating copy to clipboard operation
oauth2-client copied to clipboard

Fix: sending JSON or multipart requests

Open fverry opened this issue 2 years ago • 3 comments

Sending url-encoded params, json or multipart requests with Guzzle is easy, because shortcuts are implemented within GuzzleHttp\Client::applyOptions().

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.

Upgrading AbstractProvider::getResponse() to support this second optional parameter allows the developper to use a "form_params", "json" or "multipart" option key :

$options = ['multipart' => $multipart];
$request = $oauth2client->getAuthenticatedRequest($method, $url, $token);
$response = $oauth2client->getResponse($request, $options);

See also

Fixes: #935 #985 Duplicates: #967


For immediate use

Run the following on composer CLI:

composer config repositories.oauth2-client vcs https://github.com/fverry/oauth2-client
composer require "league/oauth2-client:dev-httpclientinterface-send-options as 2.x-dev"

Workaround

Use:

$request = $oauth2client->getAuthenticatedRequest($method, $url, $token);
$oauth2client->getHttpClient()->send($request, $options);

Instead of:

$request = $oauth2client->getAuthenticatedRequest($method, $url, $token, $options);
$oauth2client->getResponse($request);

fverry avatar Mar 25 '23 12:03 fverry

@fverry, thank you for submitting this PR. Please take a look at the failing tests and fix those. Then, we'll accept this.

ramsey avatar Apr 16 '23 17:04 ramsey

I'd note that this is an issue for query parameters as well as using json.

@fverry I'm wondering why you chose this as the fix though. Would it not be better to fix RequestFactory::getRequestWithOptions to use GuzzleHttp\Client::applyOptions() instead of cherry-picking only headers, body, and version?

isleshocky77 avatar Nov 29 '23 22:11 isleshocky77

Wow thank god I found this thread because I was having a hell of a time getting POST requests to work. $oauth2client->getHttpClient()->send($request, $options); saved the day.

gavin310 avatar Feb 28 '24 14:02 gavin310