Skip to content

Commit bd5fef5

Browse files
committed
feature #14338 [Mime] Add alternative FormDataPart array structure (HypeMC)
This PR was merged into the 5.x branch. Discussion ---------- [Mime] Add alternative FormDataPart array structure Resolves #14322 Commits ------- 1a56331 Add alternative FormDataPart array structure
2 parents 3facbc5 + 1a56331 commit bd5fef5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

http_client.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,35 @@ 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+
647+
.. versionadded:: 5.2
648+
649+
The alternative array structure was introduced in Symfony 5.2.
650+
622651
By default, HttpClient streams the body contents when uploading them. This might
623652
not work with all servers, resulting in HTTP status code 411 ("Length Required")
624653
because there is no ``Content-Length`` header. The solution is to turn the body

0 commit comments

Comments
 (0)