CodeIgniter Forums
curlrequest not updating the headers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: curlrequest not updating the headers (/showthread.php?tid=79444)



curlrequest not updating the headers - chakycool - 06-17-2021

Hi, 
It seems that the "curlrequest" is not updating the header when I make 2 API calls right after another. One API call to get the token (with 2 header items) then again call the second API with the Token in the header (3 header items).

Second API call keeps failing as the header is not getting updated.

To test this I got the Token from the 1st and Hard corded it to the second and disable the 1st call, then it worked.



$client = \Config\Services::curlrequest();

$body = '{"username":"[email protected]","password" : "'.$keys->pw.'"}';

/// 1st call
$result = $client->setBody($body)->post($keys->url.'/v1/login/', [
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => $keys->key
]
]);
$info = json_decode($result->getBody());

///2nd call

$result2 = $client->get($keys->url.'/v1/members/data/'.$member_id, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'JWT '.$info->token,
'x-api-key' => $keys->key
],
'debug' => true,
'http_errors' => false

]);


Please help!


RE: curlrequest not updating the headers - chakycool - 06-19-2021

Found the problem.

\Config\Services::curlrequest() instant is shared, so it want get updated. Can some one please tell me how to pass false on this so it will load a new instance on the second round.


RE: curlrequest not updating the headers - chakycool - 06-20-2021

Found the solution to my problem.
Any body else facing this load the service as below.

$client = \Config\Services::curlrequest(array(),null,null,false);


RE: curlrequest not updating the headers - jreklund - 06-20-2021

Personally I would create it without the service, I think it's cleaner.

Code:
$options = []; // optional.

$client = new \CodeIgniter\HTTP\CURLRequest(
        new \Config\App(),
        new \CodeIgniter\HTTP\URI(),
        new \CodeIgniter\HTTP\Response(new \Config\App()),
        $options
);



RE: curlrequest not updating the headers - paulbalandan - 06-20-2021

You can also use `single_service()` so that you're assured that the resulting object is always a fresh instance.


RE: curlrequest not updating the headers - chakycool - 06-20-2021

how did I not see this on the docs  Sad

single_service() is so easy. thanks paulbalanda  Big Grin