Skip to content

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

Open
@RomainMazB

Description

@RomainMazB

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions