Skip to content

Commit

Permalink
Fix Content-Length for POST when data empty
Browse files Browse the repository at this point in the history
Signed-off-by: Jacques ROUSSEL <[email protected]>
  • Loading branch information
rouja committed Oct 28, 2023
1 parent 76de900 commit 5a2c61e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ protected function prepareHeaders($body, $uri)
$headers['Content-Length'] = strlen($body);
}
}
else {
if ($this->getMethod() == 'POST') {
$headers['Content-Length'] = 0;
}
}


// Merge the headers of the request (if any)
// here we need right 'http field' and not lowercase letters
Expand Down
16 changes: 16 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,22 @@ public function testClientRequestMethod()
$this->assertSame(Client::ENC_URLENCODED, $client->getEncType());
}

public function testClientEmptyPost()
{
$client = new Client();
$prepareHeadersReflection = new ReflectionMethod($client, 'prepareHeaders');
$prepareHeadersReflection->setAccessible(true);
$request = new Request();
$request->setMethod(Request::METHOD_POST);
$client->setRequest($request);
$this->assertSame($client->getRequest(), $request);
$headers = $prepareHeadersReflection->invoke($client, '', new Http('http://localhost:5984'));
$this->assertIsArray($headers);
$this->assertArrayHasKey('Content-Length', $headers);
$this->assertSame($headers['Content-Length'], 0);

}

/**
* @group 7332
*/
Expand Down

0 comments on commit 5a2c61e

Please sign in to comment.