DarkFenix
DarkFenix

Reputation: 746

Getting the value of a cookie from an HTTP call using GuzzleHttp

I am now using GuzzleHttp to make HTTP requests, first I make a POST request to login.asp, which returns a response with Set-Cookie with a value that I need for future requests

enter image description here

When I inspect my obtained answer I get the following

enter image description here

As noted, I get all the keys except the Set-Cookie, what can be happening? How can I get this value? I'm using "guzzlehttp/guzzle": "^6.3", or can I get it using another tool?

    $jar = new CookieJar;

    $client = new Client([
        'base_uri' =>'miurl/',
        'timeout'  => 10.0,
        'cookies' => $jar
    ]);

    $response = $client->request('POST', 'login.asp',  [
        'form_params' => [
            'pws' => '',//data password
            'user' => '',//data user
        ]
    ]);

    //Request require coookies

    $response = $client->request('POST', 'goform/Wls',  [
        'form_params' => [
            /*Form´Params*/
        ],
       //if I manually add a correct userid the post application works fine
        'headers' => [
            //Require cookie param userid 
            'Cookie' => 'LANG_COOKIE=lang_span; userid=1524324306',
        ]
    ]);

Alternatively, I used this configuration without being able to obtain the cookie yet

checking a bit the answer using postman, is that after doing the correct login is still on the same page but with javascript redirect, can this influence?

<script language='JavaScript'>window.location='/admin/cable-Systeminfo.asp';</script>
</html>

The requests I make directly for a router hitron technologies cgnv22 to manage the mac filtering, I would like to provide more information but it is sensitive information

Upvotes: 5

Views: 6332

Answers (1)

Gustavo Straube
Gustavo Straube

Reputation: 3861

It seems you're making the request the right way, passing an instance of CookieJarInterface. However, you shouldn't expect the Set-Cookie header. Instead, inspect your jar to check what cookies were returned.

The following example shows how you can iterate over all cookies:

$client = new \GuzzleHttp\Client();

$jar = new \GuzzleHttp\Cookie\CookieJar();
$request = $client->request('GET', 'https://www.google.com/', [
    'cookies' => $jar
]);

$it = $jar->getIterator();
while ($it->valid()) {
    var_dump($it->current());
    $it->next();
}

Here is a sample output from the snippet above:

object(GuzzleHttp\Cookie\SetCookie)#36 (1) {
  ["data":"GuzzleHttp\Cookie\SetCookie":private]=>
  array(9) {
    ["Name"]=>
    string(3) "NID"
    ["Value"]=>
    string(132) "130=dmyl6v*******"
    ["Domain"]=>
    string(11) ".google.com"
    ["Path"]=>
    string(1) "/"
    ["Max-Age"]=>
    NULL
    ["Expires"]=>
    int(1542242169)
    ["Secure"]=>
    bool(false)
    ["Discard"]=>
    bool(false)
    ["HttpOnly"]=>
    bool(true)
  }
}

Refer to the CookieJar class source for more information on how you can access the returned cookies. Additionally, you can take a look at the docs for ArrayIterator class.

Upvotes: 9

Related Questions