Skip to content

Commit b83d683

Browse files
[HttpClient] always send Content-Type when a body is passed
1 parent 6e4b3e0 commit b83d683

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

CurlHttpClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ public function request(string $method, string $url, array $options = []): Respo
242242

243243
if ('POST' !== $method) {
244244
$curlopts[\CURLOPT_UPLOAD] = true;
245+
246+
if (!isset($options['normalized_headers']['content-type'])) {
247+
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type: application/x-www-form-urlencoded';
248+
}
245249
}
246250
} elseif ('' !== $body || 'POST' === $method || $hasContentLength) {
247251
$curlopts[\CURLOPT_POSTFIELDS] = $body;
248-
249-
if ('' === $body && 'POST' !== $method && !isset($options['normalized_headers']['content-type'])) {
250-
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
251-
}
252252
}
253253

254254
if ($options['peer_fingerprint']) {

NativeHttpClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ public function request(string $method, string $url, array $options = []): Respo
8080
}
8181
}
8282

83-
$sendContentLength = !\is_string($options['body']) || 'POST' === $method;
83+
$hasContentLength = isset($options['normalized_headers']['content-length']);
84+
$hasBody = '' !== $options['body'] || 'POST' === $method || $hasContentLength;
8485

8586
$options['body'] = self::getBodyAsString($options['body']);
8687

87-
if ('' === $options['body'] && $sendContentLength && !isset($options['normalized_headers']['content-length'])) {
88+
if ('' === $options['body'] && $hasBody && !$hasContentLength) {
8889
$options['headers'][] = 'Content-Length: 0';
8990
}
90-
if (('' !== $options['body'] || 'POST' === $method) && !isset($options['normalized_headers']['content-type'])) {
91+
if ($hasBody && !isset($options['normalized_headers']['content-type'])) {
9192
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
9293
}
9394

0 commit comments

Comments
 (0)