Skip to content

Commit

Permalink
[HttpClient] Don't override header if is x-www-form-urlencoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Oipnet authored and nicolas-grekas committed Oct 17, 2022
1 parent c8c887f commit 87d846f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/HttpClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 87d846f

Please sign in to comment.