[Form] Adding DataPart to manually set the Content-Type

Page: https://symfony.com/doc/6.4/http_client.html#uploading-data
This commit is contained in:
Thomas Landauer
2025-05-13 18:49:03 +02:00
committed by GitHub
parent 621b57b99e
commit aa84c7d467

View File

@@ -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