codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Setting CURLRequest JSON options will override body on different instance #7839

Closed rithgroove closed 1 year ago

rithgroove commented 1 year ago

PHP Version

8.2

CodeIgniter4 Version

4.3.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

no Database

What happened?

Hi, I found this behavior accidentally where once a request was sent using "json" parameters, the next curl request will have it's "body" parameters overriden by the previous request configuration.

This might relate to CURLRequest.php line 638 or parseOptions methods on CURLRequest.php.

Here's a snippet of codes to visualize the problem: CI4 3 6 bug

Steps to Reproduce

Sent 2 different request using 2 different CurlRequest Instance.

<?php

namespace App\Controllers;
use CodeIgniter\Controller;

class Home extends Controller
{
    public function index()
    {
        return view('welcome_message');
    }

    public function printBody(){
        print($this->request->getBody());
    }

    public function testCurl(){
        // Fixed variables (url etc)
        $url = "http://localhost:8083/printBody"; // url for the printbody function above

        // Text Body 1
        $text_body_1 = ["data"=> "testing", "message" => "This the 1st request body"];
        print("First Body = '".json_encode($text_body_1)."'<br>");

        // Text Body 2
        $text_body_2 = "This is the 2nd Request Body";
        print("Second Body = '".$text_body_2."'<br>");

        // First Request
        $client_1 =  \Config\Services::curlrequest();
        $response_1 = $client_1->request(
            'post',
            $url,
            [
                "json" => $text_body_1,
                'http_errors' => false
            ],
        );
        print("First Response = '".$response_1->getBody()."'<br>");

        // Second Request
        $client_2 =  \Config\Services::curlrequest();
        $response_2 = $client_2->request(
            'post',
            $url,
            [
                "body" => $text_body_2,
                'http_errors' => false
            ],
        );
        print("Second Response = '".$response_2->getBody()."'<br>");

    }

}

Expected Output

the second request should send the newly set http request body

Anything else?

No response

neznaika0 commented 1 year ago

$shareOptions set as false?

rithgroove commented 1 year ago

@neznaika0
$shareOptions = true;

I just found out about that. Thanks :)

kenjis commented 1 year ago

This is not a bug. See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#sharing-options