diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index c8aceb9cdac0..db794d5e907a 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -151,6 +151,9 @@ protected function resetOptions() $this->headers = []; $this->headerMap = []; + // Reset body + $this->body = null; + // Reset configs $this->config = $this->defaultConfig; diff --git a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php index 7f7d20209ce2..e6e7449d2987 100644 --- a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php +++ b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php @@ -807,6 +807,21 @@ public function testApplyBody() $this->assertSame('name=George', $request->curl_options[CURLOPT_POSTFIELDS]); } + public function testBodyIsResetOnSecondRequest() + { + $request = $this->getRequest([ + 'base_uri' => 'http://www.foo.com/api/v1/', + 'delay' => 100, + ]); + $request->setBody('name=George'); + $request->setOutput('Hi there'); + + $request->post('answer'); + $request->post('answer'); + + $this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $request->curl_options); + } + public function testResponseHeaders() { $request = $this->getRequest([ @@ -922,7 +937,10 @@ public function testJSONData() $this->assertSame('post', $this->request->getMethod()); $expected = json_encode($params); - $this->assertSame($expected, $this->request->getBody()); + $this->assertSame( + $expected, + $this->request->curl_options[CURLOPT_POSTFIELDS] + ); } public function testSetJSON() @@ -936,7 +954,12 @@ public function testSetJSON() ]; $this->request->setJSON($params)->post('/post'); - $this->assertSame(json_encode($params), $this->request->getBody()); + $expected = json_encode($params); + $this->assertSame( + $expected, + $this->request->curl_options[CURLOPT_POSTFIELDS] + ); + $this->assertSame( 'Content-Type: application/json', $this->request->curl_options[CURLOPT_HTTPHEADER][0] diff --git a/user_guide_src/source/libraries/curlrequest.rst b/user_guide_src/source/libraries/curlrequest.rst index b77d41e6b088..84995839ecdb 100644 --- a/user_guide_src/source/libraries/curlrequest.rst +++ b/user_guide_src/source/libraries/curlrequest.rst @@ -28,12 +28,14 @@ Sharing Options Due to historical reasons, by default, the CURLRequest shares all the options between requests. If you send more than one request with an instance of the class, -this behavior may cause an error request with unnecessary headers. +this behavior may cause an error request with unnecessary headers and body. You can change the behavior by editing the following config parameter value in **app/Config/CURLRequest.php** to ``false``: .. literalinclude:: curlrequest/001.php +.. note:: Before v4.2.0, the request body is not reset even if ``$shareOptions`` is false due to a bug. + ******************* Loading the Library *******************