8000 [HttpClient] Improve default content-type handling · symfony/symfony@91099a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91099a9

Browse files
[HttpClient] Improve default content-type handling
1 parent 7124c3a commit 91099a9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9797
}
9898

9999
if (isset($options['body'])) {
100+
if (\is_array($options['body'])) {
101+
$options['normalized_headers']['content-type'] = ['Content-Type: application/x-www-form-urlencoded'];
102+
}
103+
100104
$options['body'] = self::normalizeBody($options['body']);
101105

102106
8000 if (\is_string($options['body'])

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ public function provideCurlRequests(): iterable
252252
--request POST \\
253253
--url %1$shttp://localhost:8057/json%1$s \\
254254
--header %1$sAccept: */*%1$s \\
255-
--header %1$sContent-Length: 32%1$s \\
256255
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
256+
--header %1$sContent-Length: 32%1$s \\
257257
--header %1$sAccept-Encoding: gzip%1$s \\
258258
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
259259
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s',

src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,22 @@ public function getRedirectWithAuthTests()
455455
'other host' => ['url' => 'http://127.0.0.1:8057/302', 'redirectWithAuth' => false],
456456
];
457457
}
458+
459+
public function testDefaultContentType()
460+
{
461+
$client = $this->getHttpClient(__FUNCTION__);
462+
$client = $client->withOptions(['headers' => ['Content-Type: application/json']]);
463+
464+
$response = $client->request('POST', 'http://localhost:8057/post', [
465+
'body' => ['abc' => 'def'],
466+
]);
467+
468+
$this->assertSame(['abc' => 'def', 'REQUEST_METHOD' => 'POST'], $response->toArray());
469+
470+
$response = $client->request('POST', 'http://localhost:8057/post', [
471+
'body' => '{"abc": "def"}',
472+
]);
473+
474+
$this->assertSame(['abc' => 'def', 'content-type' => 'application/json', 'REQUEST_METHOD' => 'POST'], $response->toArray());
475+
}
458476
}

0 commit comments

Comments
 (0)
0