Open
Description
Description
As of today, each part of a request using the multipart option should either:
- be a resource
- be a string
- implements StreamInterface
But a lot of users naturally expect to be able to pass an array, issues are created since 2015 with this natural expectation:
- Multipart with an array form value?
- Send files and multidimensional array with Guzzle 6
- How to send duplicate keys in post data?
- How to pass multidimensional Array and multi files ?
Example
$client->request('POST', '/post', [
'multipart' => [
[
'name' => 'products',
'contents' => [
['id' => 123, 'quantity' => 1],
['id' => 456, 'quantity' => 2],
['id' => 789, 'quantity' => 3]
]
]
]);
Additional context
I faced this issue when using Guzzle through the Laravel Http
facade.
I tried to fix this from the fw side but Taylor Otwell suggested to fix this inside Guzzle directly.
I continued to investigate on this on both Guzzle, Laravel and even other fw way to tackle this, so I am able to do a PR to add this feature.
Basically it would expand the array values into multiple parts, the below request would results to this one:
$client->request('POST', '/post', [
'multipart' => [
[
'name' => 'products[0][id]',
'contents' =>'123'
],
[
'name' => 'products[0][quantity]',
'contents' =>'1'
],
[
'name' => 'products[1][id]',
'contents' =>'456'
],
[
'name' => 'products[1][quantity]',
'contents' =>'2'
],
[
'name' => 'products[2][id]',
'contents' =>'789'
],
[
'name' => 'products[2][quantity]',
'contents' => '3'
]
]);