CodeIgniter Forums
curlrequest - help - 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 - help (/showthread.php?tid=79924)



curlrequest - help - chakycool - 08-15-2021

HI,

I'm trying to call a REST API using the curlrequest service and I think I go the post syntax wrong. Can someone help me.

PHP Code:
$client single_service('curlrequest');
 
$result $client->post($keys->url.'/v2/', [
                    'headers' => [
                              'Content-Type' => 'application/json',
                                'Authorization' => 'JWT '.$access->token,
                                'x-api-key'    => $keys->key
                                
],
                    'body' => $payload,
                    'debug' => true              
                
]); 

This syntax is not posting the body payload, everything else is fine.


RE: curlrequest - help - InsiteFX - 08-17-2021

From looking at the service code your missing the options array when getting the service. that's where the base_uri is setup.

Look at the code in system/Config/Services.php


RE: curlrequest - help - chakycool - 08-19-2021

I'm not defining the base_uri but passing the full URL. Below is the updated code.

$keys->url = "https://dommain.com"
$client = single_service('curlrequest');
$result = $client->post($keys->url.'/v2/', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'JWT '.$access->token,
'x-api-key' => $keys->key
],
'body' => $payload,
'debug' => true
]);

I tried the below method and it worked.
$client->setBody($payload)->post($keys->url.'/v2/',...................

Looks like we can't pass the body payload in the option array.