Skip to content

Commit 87d846f

Browse files
Oipnetnicolas-grekas
authored andcommitted
[HttpClient] Don't override header if is x-www-form-urlencoded
1 parent c8c887f commit 87d846f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

HttpClientTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9797
}
9898

9999
if (isset($options['body'])) {
100-
if (\is_array($options['body'])) {
100+
if (\is_array($options['body']) && !isset($options['normalized_headers']['content-type'])) {
101101
$options['normalized_headers']['content-type'] = ['Content-Type: application/x-www-form-urlencoded'];
102102
}
103103

Tests/HttpClientTraitTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ public function providePrepareRequestUrl(): iterable
4949
yield ['http://example.com/?b=', 'http://example.com/', ['a' => null, 'b' => '']];
5050
}
5151

52+
public function testPrepareRequestWithBodyIsArray()
53+
{
54+
$defaults = [
55+
'base_uri' => 'http://example.com?c=c',
56+
'query' => ['a' => 1, 'b' => 'b'],
57+
'body' => []
58+
];
59+
[, $defaults] = self::prepareRequest(null, null, $defaults);
60+
61+
[,$options] = self::prepareRequest(null, 'http://example.com', [
62+
'body' => [1, 2],
63+
'headers' => [
64+
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
65+
]
66+
], $defaults);
67+
68+
$this->assertContains('Content-Type: application/x-www-form-urlencoded; charset=utf-8', $options['headers']);
69+
}
70+
5271
/**
5372
* @dataProvider provideResolveUrl
5473
*/

0 commit comments

Comments
 (0)