diff --git a/src/Client.php b/src/Client.php index 2d144102..230e8101 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1256,6 +1256,12 @@ protected function prepareHeaders($body, $uri) $headers['Content-Length'] = strlen($body); } } + else { + if ($this->getMethod() === 'POST' || $this->getMethod() === 'PUT') { + $headers['Content-Length'] = 0; + } + } + // Merge the headers of the request (if any) // here we need right 'http field' and not lowercase letters diff --git a/test/ClientTest.php b/test/ClientTest.php index 21dd8770..52dbc4f8 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -505,6 +505,29 @@ public function testClientRequestMethod() $this->assertSame(Client::ENC_URLENCODED, $client->getEncType()); } + public function testClientEmptyPostPut() + { + $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); + $request = new Request(); + $request->setMethod(Request::METHOD_PUT); + $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 */