Welcome Guest, Not a member yet? Register   Sign In
curlrequest not updating the headers
#1

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!
Reply
#2

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.
Reply
#3

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

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

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
);
Reply
#5

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

(This post was last modified: 06-20-2021, 05:29 AM by chakycool.)

how did I not see this on the docs  Sad

single_service() is so easy. thanks paulbalanda  Big Grin
Reply




Theme © iAndrew 2016 - Forum software by © MyBB