Skip to content

[HttpClient] Move Content-Type after Content-Length #45813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
unset($options['json']);

if (!isset($options['normalized_headers']['content-type'])) {
$options['normalized_headers']['content-type'] = [$options['headers'][] = 'Content-Type: application/json'];
$options['normalized_headers']['content-type'] = ['Content-Type: application/json'];
}
}

if (!isset($options['normalized_headers']['accept'])) {
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: */*'];
$options['normalized_headers']['accept'] = ['Accept: */*'];
}

if (isset($options['body'])) {
Expand All @@ -92,7 +92,6 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
&& ('' !== $h || ('' !== $options['body'] && !isset($options['normalized_headers']['transfer-encoding'])))
) {
$options['normalized_headers']['content-length'] = [substr_replace($h ?: 'Content-Length: ', \strlen($options['body']), 16)];
$options['headers'] = array_merge(...array_values($options['normalized_headers']));
}
}

Expand Down Expand Up @@ -134,11 +133,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
if (null !== $url) {
// Merge auth with headers
if (($options['auth_basic'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Basic '.base64_encode($options['auth_basic'])];
$options['normalized_headers']['authorization'] = ['Authorization: Basic '.base64_encode($options['auth_basic'])];
}
// Merge bearer with headers
if (($options['auth_bearer'] ?? false) && !($options['normalized_headers']['authorization'] ?? false)) {
$options['normalized_headers']['authorization'] = [$options['headers'][] = 'Authorization: Bearer '.$options['auth_bearer']];
$options['normalized_headers']['authorization'] = ['Authorization: Bearer '.$options['auth_bearer']];
}

unset($options['auth_basic'], $options['auth_bearer']);
Expand All @@ -161,6 +160,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt

$options['max_duration'] = isset($options['max_duration']) ? (float) $options['max_duration'] : 0;

if (isset($options['normalized_headers']['content-length']) && $contentType = $options['normalized_headers']['content-type'] ?? null) {
// Move Content-Type after Content-Length, see https://bugs.php.net/44603
unset($options['normalized_headers']['content-type']);
$options['normalized_headers']['content-type'] = $contentType;
}

$options['headers'] = array_merge(...array_values($options['normalized_headers']));

return [$url, $options];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testMatchingUrlsAndOptions()

$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
$requestOptions = $response->getRequestOptions();
$this->assertSame('Content-Type: application/json', $requestOptions['headers'][1]);
$this->assertSame('Content-Type: application/json', $requestOptions['headers'][3]);
$requestJson = json_decode($requestOptions['body'], true);
$this->assertSame('http://example.com', $requestJson['url']);
$this->assertSame('X-FooBar: '.$defaultOptions['.*/foo-bar']['headers']['X-FooBar'], $requestOptions['headers'][0]);
Expand Down