From 1a56331bee4daf037f09b1a0e991f9ef9c5d539c Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 4 Oct 2020 18:34:50 +0200 Subject: [PATCH] Add alternative FormDataPart array structure --- http_client.rst | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/http_client.rst b/http_client.rst index 7f44141a6dc..240d28d1e73 100644 --- a/http_client.rst +++ b/http_client.rst @@ -619,6 +619,35 @@ according to the ``multipart/form-data`` content-type. The 'body' => $formData->bodyToIterable(), ]); +.. tip:: + + When using multidimensional arrays the :class:`Symfony\\Component\\Mime\\Part\\Multipart\\FormDataPart` + automatically appends ``[key]`` to the name of the field:: + + $formData = new FormDataPart([ + 'array_field' => [ + 'some value', + 'other value', + ], + ]); + + $formData->getParts(); // Returns two instances of TextPart + // with the names "array_field[0]" and "array_field[1]" + + This behavior can be bypassed by using the following array structure:: + + $formData = new FormDataPart([ + ['array_field' => 'some value'], + ['array_field' => 'other value'], + ]); + + $formData->getParts(); // Returns two instances of TextPart both + // with the name "array_field" + + .. versionadded:: 5.2 + + The alternative array structure was introduced in Symfony 5.2. + By default, HttpClient streams the body contents when uploading them. This might not work with all servers, resulting in HTTP status code 411 ("Length Required") because there is no ``Content-Length`` header. The solution is to turn the body