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

Accepts multi-dimensional arrays as value for formdata field value #3260

Open
RomainMazB opened this issue Oct 7, 2024 · 0 comments
Open

Comments

@RomainMazB
Copy link

RomainMazB commented Oct 7, 2024

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:

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'
    ]
]);
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
@RomainMazB and others