Skip to content

Commit f06092c

Browse files
Merge branch '4.4' into 5.4
* 4.4: [HttpClient] fix sending Content-Length/Type for POST
2 parents ef69fbe + 375a315 commit f06092c

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

AmpHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function request(string $method, string $url, array $options = []): Respo
9292
}
9393
}
9494

95-
if ('' !== $options['body'] && !isset($options['normalized_headers']['content-type'])) {
95+
if (('' !== $options['body'] || 'POST' === $method) && !isset($options['normalized_headers']['content-type'])) {
9696
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
9797
}
9898

CurlHttpClient.php

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

252-
if ('' === $body && !isset($options['normalized_headers']['content-type'])) {
252+
if ('' === $body && 'POST' !== $method && !isset($options['normalized_headers']['content-type'])) {
253253
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
254254
}
255255
}

NativeHttpClient.php

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

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

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

Tests/HttpClientTestCase.php

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

404404
$response = $client->request('POST', 'http://localhost:8057/302/relative', [
405-
'body' => 'abc',
405+
'body' => '',
406406
]);
407407

408408
$this->assertSame(200, $response->getStatusCode());
409+
$this->assertStringContainsStringIgnoringCase("\r\nContent-Length: 0", $response->getInfo('debug'));
409410
}
410411

411412
public function testEmptyPut()

0 commit comments

Comments
 (0)