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

Create multipart/form-data requests? #985

Open
Lindstromer opened this issue Jan 24, 2023 · 1 comment
Open

Create multipart/form-data requests? #985

Lindstromer opened this issue Jan 24, 2023 · 1 comment

Comments

@Lindstromer
Copy link

I'm having issues creating a request with an attached file. Tried a lot of different alternatives but always end up with issues.

Seems like Guzzle in itself handles this, but the oauth2-client is limiting the getAuthenticatedRequest method to using only " Any of "headers", "body", and "protocolVersion".".

Example where it says to not specify form-data:
https://stackoverflow.com/questions/47550801/multiple-files-uploaded-via-guzzle-multipart-form-data-request-are-not-recognize

Another discussion regarding this, I tried following it but cannot get it to work. Guzzle complains about it being an invalid resource type "array" while doing so:
https://stackoverflow.com/questions/58736361/replicate-curl-multipart-form-data-request-with-guzzle-league-oauth2-client?rq=1

Error is thrown in the streamFor method found in Utils.php located in guzzlehttp vendor/psr7/src/Utils.php.

Anyone tried this and got it to work? Any help would be very appreciated.

@fverry
Copy link

fverry commented Mar 25, 2023

You can make it work with this following extra code :

if (isset($options['multipart']))
{
	$multipartStream = new GuzzleHttp\Psr7\MultipartStream($options['multipart']);
	$boundary = $multipartStream->getBoundary();
	$options['headers']['content-type'] = "multipart/form-data; boundary={$boundary}";
	$options['body'] = $multipartStream;
	unset($options['multipart']);
}

It is inspired from GuzzleHttp\Client::applyOptions() which you should read.

As stated by this stackoverflow answer, the only keys allowed in League\OAuth2\Client\Provider\AbstractProvider::getAuthenticatedRequest()'s $options are "headers", "body", and "protocolVersion".

Thus in the following code, the "multipart" key in the $options array will be ignored.

$multipart = [];
$multipart[] = [
	'name'     => "documents[{$key}]",
	'contents' => GuzzleHttp\Psr7\Utils::tryFopen($file['tmp_name'], 'r'),
	'filename' => $file['name'],
];
$options['multipart'] = $multipart;
$request = $provider->getAuthenticatedRequest($method, $url, $token, $options);

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

No branches or pull requests

2 participants