Welcome Guest, Not a member yet? Register   Sign In
curlrequest - help
#1

(This post was last modified: 08-15-2021, 11:36 PM by chakycool.)

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

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB