Skip to content

Commit db5bddb

Browse files
committed
Add alternative FormDataPart array structure
1 parent f9c2a32 commit db5bddb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

http_client.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,31 @@ according to the ``multipart/form-data`` content-type. The
619619
'body' => $formData->bodyToIterable(),
620620
]);
621621

622+
.. tip::
623+
624+
When using multidimensional arrays the :class:`Symfony\\Component\\Mime\\Part\\Multipart\\FormDataPart`
625+
automatically appends ``[key]`` to the name of the field::
626+
627+
$formData = new FormDataPart([
628+
'array_field' => [
629+
'some value',
630+
'other value',
631+
],
632+
]);
633+
634+
$formData->getParts(); // Returns two instances of TextPart
635+
// with the names "array_field[0]" and "array_field[1]"
636+
637+
This behavior can be bypassed by using the following array structure::
638+
639+
$formData = new FormDataPart([
640+
['array_field' => 'some value'],
641+
['array_field' => 'other value'],
642+
]);
643+
644+
$formData->getParts(); // Returns two instances of TextPart both
645+
// with the name "array_field"
646+
622647
By default, HttpClient streams the body contents when uploading them. This might
623648
not work with all servers, resulting in HTTP status code 411 ("Length Required")
624649
because there is no ``Content-Length`` header. The solution is to turn the body

0 commit comments

Comments
 (0)