Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: sending JSON or multipart requests #995

Closed

Conversation

fverry
Copy link

@fverry fverry commented Mar 25, 2023

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);

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](guzzle/guzzle@1a9ad6b?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);
```
@ramsey
Copy link
Contributor

ramsey commented Apr 16, 2023

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

@isleshocky77
Copy link

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?

@gavin310
Copy link

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.

@ramsey
Copy link
Contributor

ramsey commented Dec 11, 2024

Closing this in favor of focusing on #1032 for the next major version of this library.

Thank you for contributing! 🎉

@ramsey ramsey closed this Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

how do i pass $_POST parameters with "getAuthenticatedRequest()"?
4 participants