From 7aa2c446fcf4129c0640065b9421ef183f9a1289 Mon Sep 17 00:00:00 2001 From: Arnaud POINTET Date: Thu, 13 Oct 2022 16:05:18 +0200 Subject: [PATCH] [HttpClient] Don't override header if is x-www-form-urlencoded --- .../Component/HttpClient/HttpClientTrait.php | 2 +- .../HttpClient/Tests/HttpClientTraitTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/HttpClientTrait.php b/src/Symfony/Component/HttpClient/HttpClientTrait.php index a2ed4bc71e8b1..e35bacbec7e00 100644 --- a/src/Symfony/Component/HttpClient/HttpClientTrait.php +++ b/src/Symfony/Component/HttpClient/HttpClientTrait.php @@ -97,7 +97,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt } if (isset($options['body'])) { - if (\is_array($options['body'])) { + if (\is_array($options['body']) && !isset($options['normalized_headers']['content-type'])) { $options['normalized_headers']['content-type'] = ['Content-Type: application/x-www-form-urlencoded']; } diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php index b811626c0c670..81bc7447a1bd4 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php @@ -49,6 +49,25 @@ public function providePrepareRequestUrl(): iterable yield ['http://example.com/?b=', 'http://example.com/', ['a' => null, 'b' => '']]; } + public function testPrepareRequestWithBodyIsArray() + { + $defaults = [ + 'base_uri' => 'http://example.com?c=c', + 'query' => ['a' => 1, 'b' => 'b'], + 'body' => [] + ]; + [, $defaults] = self::prepareRequest(null, null, $defaults); + + [,$options] = self::prepareRequest(null, 'http://example.com', [ + 'body' => [1, 2], + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' + ] + ], $defaults); + + $this->assertContains('Content-Type: application/x-www-form-urlencoded; charset=utf-8', $options['headers']); + } + /** * @dataProvider provideResolveUrl */