Welcome Guest, Not a member yet? Register   Sign In
What's the interest of setting CURLOPT_HEADER to true by default?
#1

Hello,

I was working for the first time with the CurlRequestClass today and found out that CURLOPT_HEADER was at true by default in CURLRequest.php. As a reminder this makes CURL set some headers in the body response which can be unhandy while working with REST API for example.

I wonder what's the main point of this and, if there's any, why isn't there any method in the library to set the curl options ourselves?  Smile
I might aswell miss something.

Thanks for your responses.
Reply
#2

Too my knowledge it's set so that we can get for example the status code back from the server. So we know it have been succeeded or denied.

Take a look at setResponseHeaders().
Reply
#3

To go further I identified the problem I was running into.

Codeigniter Curl lib uses the header sent in the body response with CURLOPT_HEADER = true to actually set the header of the Response object. However, the API I was using has a Digest Authentification and that's where the problem comes from.
To make it short, Digest Auth forces you to make a first call to get some infos from the server and a second one to get your desired data without you doing anything. This makes you with a curl response which looks like this :

Code:
HTTP/1.1 401 Unauthorized
FIRST CALL IS 401 BECAUSE I'M NOT LOGGED.
CLIENT IS GETTING INFO FROM THE SERVER.
// first headers go here

HTTP/1.1 200 OK
SECOND HTTP CALL IS OK, I PROVIDED AUTH RESOURCE
// second headers go here

MY RESULT IS HERE

Problem is from the CURLRequest lib starting at line 458

PHP Code:
// Split out our headers and body
$break strpos($output"\r\n\r\n");

if (
$break !== false)
{
    
// Our headers
    
$headers explode("\n"substr($output0$break));

    
$this->setResponseHeaders($headers);

    
// Our body
    
$body substr($output$break 4);
    
$this->response->setBody($body);


This basically go to the first double carriage return and set the first occurence to the headers and the second one to the body.
Because I'm using digest auth and having a double headers in my curl response, it set the first response as my header (which is the wrong one) and keep the good header in my body response. The lib should ignore the first header response in case of a digest auth.

I'm lucky enough to own the source code of the server I was calling so I made it to a basic auth and everything's fine.

Can someone confirm this is a bug from the lib or maybe the digest auth was not properly handled from the server side?
Reply
#4

Got the same error.

I need to make the 2 API calls. 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

]);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB