Skip to content

Commit ea41b93

Browse files
[HttpClient] fix sending PUT requests with curl
1 parent 12f5708 commit ea41b93

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CurlHttpClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,12 @@ public function request(string $method, string $url, array $options = []): Respo
243243
if ('POST' !== $method) {
244244
$curlopts[\CURLOPT_UPLOAD] = true;
245245
}
246-
} elseif ('' !== $body || 'POST' === $method) {
246+
} elseif ('' !== $body || 'POST' === $method || $hasContentLength) {
247247
$curlopts[\CURLOPT_POSTFIELDS] = $body;
248+
249+
if ('' === $body && !isset($options['normalized_headers']['content-type'])) {
250+
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
251+
}
248252
}
249253

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

NativeHttpClient.php

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

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

85-
if ('' !== $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-type'])) {
85+
if ('' !== $options['body'] && !isset($options['normalized_headers']['content-type'])) {
8686
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
8787
}
8888

Tests/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ public function testRedirectAfterPost()
214214
$this->assertSame(200, $response->getStatusCode());
215215
}
216216

217+
public function testEmptyPut()
218+
{
219+
$client = $this->getHttpClient(__FUNCTION__);
220+
221+
$response = $client->request('PUT', 'http://localhost:8057/post', [
222+
'headers' => ['Content-Length' => '0'],
223+
]);
224+
225+
$this->assertSame(200, $response->getStatusCode());
226+
$this->assertStringContainsString("\r\nContent-Length: ", $response->getInfo('debug'));
227+
}
228+
217229
public function testNullBody()
218230
{
219231
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)