Skip to content

Commit 764b1e0

Browse files
authored
Merge pull request #6014 from kenjis/fix-CURLRequest-body
fix: CURLRequest request body is not reset on the next request
2 parents 3719119 + 98c43f7 commit 764b1e0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

system/HTTP/CURLRequest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ protected function resetOptions()
151151
$this->headers = [];
152152
$this->headerMap = [];
153153

154+
// Reset body
155+
$this->body = null;
156+
154157
// Reset configs
155158
$this->config = $this->defaultConfig;
156159

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,21 @@ public function testApplyBody()
807807
$this->assertSame('name=George', $request->curl_options[CURLOPT_POSTFIELDS]);
808808
}
809809

810+
public function testBodyIsResetOnSecondRequest()
811+
{
812+
$request = $this->getRequest([
813+
'base_uri' => 'http://www.foo.com/api/v1/',
814+
'delay' => 100,
815+
]);
816+
$request->setBody('name=George');
817+
$request->setOutput('Hi there');
818+
819+
$request->post('answer');
820+
$request->post('answer');
821+
822+
$this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $request->curl_options);
823+
}
824+
810825
public function testResponseHeaders()
811826
{
812827
$request = $this->getRequest([
@@ -922,7 +937,10 @@ public function testJSONData()
922937
$this->assertSame('post', $this->request->getMethod());
923938

924939
$expected = json_encode($params);
925-
$this->assertSame($expected, $this->request->getBody());
940+
$this->assertSame(
941+
$expected,
942+
$this->request->curl_options[CURLOPT_POSTFIELDS]
943+
);
926944
}
927945

928946
public function testSetJSON()
@@ -936,7 +954,12 @@ public function testSetJSON()
936954
];
937955
$this->request->setJSON($params)->post('/post');
938956

939-
$this->assertSame(json_encode($params), $this->request->getBody());
957+
$expected = json_encode($params);
958+
$this->assertSame(
959+
$expected,
960+
$this->request->curl_options[CURLOPT_POSTFIELDS]
961+
);
962+
940963
$this->assertSame(
941964
'Content-Type: application/json',
942965
$this->request->curl_options[CURLOPT_HTTPHEADER][0]

user_guide_src/source/libraries/curlrequest.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ Sharing Options
2828

2929
Due to historical reasons, by default, the CURLRequest shares all the options between requests.
3030
If you send more than one request with an instance of the class,
31-
this behavior may cause an error request with unnecessary headers.
31+
this behavior may cause an error request with unnecessary headers and body.
3232

3333
You can change the behavior by editing the following config parameter value in **app/Config/CURLRequest.php** to ``false``:
3434

3535
.. literalinclude:: curlrequest/001.php
3636

37+
.. note:: Before v4.2.0, the request body is not reset even if ``$shareOptions`` is false due to a bug.
38+
3739
*******************
3840
Loading the Library
3941
*******************

0 commit comments

Comments
 (0)