-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
HTTP Client Multipart with multidimensional array inputs #37174
Comments
In the meantime, I have used the code in this answer here to reformat the POST array: https://stackoverflow.com/a/53731825/372630 It would be great if we could integrate something like this in to the library, as the error isn't helpful and it took me a while to figure out the problem. |
Any potential solution would need to take in to account:
|
Are you able to submit a PR? |
Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue and we'll re-open this issue. |
This is definitely happening: use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\File;
$path = storage_path('file.txt');
Http::acceptJson()
->attach('attachments[]', File::get($path), File::basename($path))
->post($destination, [
'name' => 'John Smith',
'providers' => [1,2,3]
]); with throw Http::acceptJson()
->attach('attachments[]', File::get($path), File::basename($path))
->post($destination, [
'name' => 'John Smith',
'providers' => '1'
]); |
Same issue here. |
This is still an issue. Please re-open. |
@mikecummings10 @renwarkurd @nickfls Have you found any fix for this issue? |
Still bugging for me on Laravel 10. |
the same in Laravel 10 |
still happening, no one seems to care about fixing this one... |
I made a PR to fix it but it sounds like it won't be merged. After more investigations on what happens and what would be the cleaner/easier way to tackle this. Here is my final thoughts. WhyWhen using Laravel client to upload a file or with the
As you can see, the HowNow this is known, you understand that it's up to us to prepare the nested array to the Guzzle format. // The data with nested array
$data = [
'products' => [
[
'id' => '123',
'quantity' => '1',
],
[
'id' => '321',
'name' => '2',
]
]
];
// Use the FormDataPart from Symfony to flatten our nested array values into a one level array
$formParts = (new \Symfony\Component\Mime\Part\Multipart\FormDataPart($data))->getParts();
// Convert each part to the name/contents Guzzle format
$guzzleData = array_map(fn ($field) => [
'name' => $field->getName(),
'contents' => $field->bodyToString()
], $formParts);
$request = Http::asMultipart()->post($url, $guzzleData); RequirementsThere is only one requirement that I faced: Symfony's class only accepts a string as value. |
Description:
The fix applied in #32058 does not work for multidimensional array inputs.
Steps To Reproduce:
HTML:
PHP:
This produces an error:
A 'contents' key is required
.A
print_r()
of$laravelData
(line 714src/Illuminate/Http/Client/PendingRequest.php
) outputs the following:As you can see, it does not format the input variables into
name
andcontents
pairs, like it has done with the file input.The text was updated successfully, but these errors were encountered: