5

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

DarkFenix
  • 706
  • 1
  • 13
  • 34
  • You may not be able to *access* the cookies, but they are probably being received, set and sent by Guzzle behind the scenes. If you want to be able to see the actual cookies (why?!) you will need to create an instance of `\GuzzleHttp\Cookie\CookieJar`. – lonesomeday Apr 21 '18 at 19:57
  • @lonesomeday I tried to do it using [that answer](https://stackoverflow.com/questions/45406915/guzzlehttphow-can-i-save-cookies-from-a-post-response-and-use-it-in-the-next-po) but it did not work – DarkFenix Apr 21 '18 at 20:00
  • did u read that? https://stackoverflow.com/questions/45406915/guzzlehttphow-can-i-save-cookies-from-a-post-response-and-use-it-in-the-next-po?rq=1 – Adam Kozlowski Apr 21 '18 at 20:12
  • @AdamKozlowski Yes, I added that clarification in my question – DarkFenix Apr 21 '18 at 20:14
  • @DarkFenix Can you be more specific than "it did not work"? What didn't work? Did you *try* doing a subsequent request, to see if the cookie was saved? Edit: the point is, you shouldn't *need* the value of the cookie, just as you don't type a cookie value in every time you go to a webpage. The client (your browser, or in this case Guzzle) should do it for you. – lonesomeday Apr 21 '18 at 20:32
  • @lonesomeday I updated my question with the final code that I tried and I could not get results, of course if I tried a POST request later but this requires the userid as a value in the request headers. – DarkFenix Apr 21 '18 at 20:41
  • @DarkFenix Are you using the same `$client` object? If you have a new object, then you need to persist your cookies in some way. The simplest is with a file – [this answer](https://stackoverflow.com/a/18707980/417562) gives a pretty good example. – lonesomeday Apr 21 '18 at 20:47
  • Yes, for all requests I use the same client object created in the first lines – DarkFenix Apr 21 '18 at 20:48
  • @DarkFenix I'm stumped in terms of Guzzle ideas. My best bet is that the cookies are not actually being sent, but it's hard to debug code on your machine! – lonesomeday Apr 21 '18 at 21:01
  • 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 – DarkFenix Apr 21 '18 at 21:05
  • Based on the screenshots, it's not possible to determine the required fields for login, and your login request doesn't include it's output. Is the login even succeeding? – drew010 May 15 '18 at 18:46
  • Yes, indeed the login is successful I can not do neither with guzzle nor with volley in android: / – DarkFenix May 15 '18 at 19:18
  • could it be that it is returned only when you make request with the browser (agent)? – Oluwatobi Samuel Omisakin May 16 '18 at 07:18
  • 1
    The current answer should help because CookieJars are there to process these headers and put them in the Jar – Tarun Lalwani May 16 '18 at 08:05
  • Is it kind of Ajax? because Ajax don't allow cookies to travel with the requests. – Rohit.007 May 18 '18 at 19:13
  • @Rohit.007 I don't think that's true. Yes, AJAX requests allow cookies. Anyway, this is a server-side request, not client-side. – Gustavo Straube May 18 '18 at 21:57

1 Answers1

9

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.

Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62