From aa84c7d4673f5523f135ebbf4b34f04172edae3f Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Tue, 13 May 2025 18:49:03 +0200 Subject: [PATCH 1/2] [Form] Adding `DataPart` to manually set the `Content-Type` Page: https://symfony.com/doc/6.4/http_client.html#uploading-data --- http_client.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/http_client.rst b/http_client.rst index 32ef552c64f..ba3c3d5a9dc 100644 --- a/http_client.rst +++ b/http_client.rst @@ -665,6 +665,15 @@ of the opened file, but you can configure both with the PHP streaming configurat $formData->getParts(); // Returns two instances of TextPart both // with the name "array_field" +Usually, the ``Content-Type`` of each form's part is detected automatically. However, +you can override it by passing a ``DataPart``:: + + use Symfony\Component\Mime\Part\DataPart; + + $formData = new FormDataPart([ + ['json_data' => new DataPart(json_encode($json), null, 'application/json')] + ]; + 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 From f8ae9b9190831f8423a0498092791ab47ff7bef1 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Tue, 13 May 2025 18:55:47 +0200 Subject: [PATCH 2/2] Update http_client.rst --- http_client.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http_client.rst b/http_client.rst index ba3c3d5a9dc..3d4f7d98c44 100644 --- a/http_client.rst +++ b/http_client.rst @@ -672,7 +672,7 @@ you can override it by passing a ``DataPart``:: $formData = new FormDataPart([ ['json_data' => new DataPart(json_encode($json), null, 'application/json')] - ]; + ]); 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")