Skip to content

Commit 375a315

Browse files
[HttpClient] fix sending Content-Length/Type for POST
1 parent ea41b93 commit 375a315

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

CurlHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function request(string $method, string $url, array $options = []): Respo
246246
} elseif ('' !== $body || 'POST' === $method || $hasContentLength) {
247247
$curlopts[\CURLOPT_POSTFIELDS] = $body;
248248

249-
if ('' === $body && !isset($options['normalized_headers']['content-type'])) {
249+
if ('' === $body && 'POST' !== $method && !isset($options['normalized_headers']['content-type'])) {
250250
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
251251
}
252252
}

NativeHttpClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public function request(string $method, string $url, array $options = []): Respo
8282

8383
$options['body'] = self::getBodyAsString($options['body']);
8484

85-
if ('' !== $options['body'] && !isset($options['normalized_headers']['content-type'])) {
85+
if ('' === $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-length'])) {
86+
$options['headers'][] = 'Content-Length: 0';
87+
}
88+
if (('' !== $options['body'] || 'POST' === $method) && !isset($options['normalized_headers']['content-type'])) {
8689
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
8790
}
8891

Tests/HttpClientTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ public function testRedirectAfterPost()
208208
$client = $this->getHttpClient(__FUNCTION__);
209209

210210
$response = $client->request('POST', 'http://localhost:8057/302/relative', [
211-
'body' => 'abc',
211+
'body' => '',
212212
]);
213213

214214
$this->assertSame(200, $response->getStatusCode());
215+
$this->assertStringContainsString("\r\nContent-Length: 0", $response->getInfo('debug'));
215216
}
216217

217218
public function testEmptyPut()

0 commit comments

Comments
 (0)