GuzzleHttp:how can I save cookies from a POST response and use it in the next POST?
Asked Answered
D

2

13

I'm using Guzzle to login my API site, and in the moment Im login with the right credentials, I get back a cookie with a RefreshToken to send it in the next call, here is my simple (and working well) code:

$client = new Client(array(
            'cookies' => true
        ));


        $response = $client->request('POST', 'http://myapi.com/login', [
            'timeout' => 30,
            'form_params' => [
                'email' => $request->get('email'),
                'password' => $request->get('password'),
            ]
        ]);

and I get back the right response with a cookie, I can see the cookie by using:

$newCookies = $response->getHeader('set-cookie');

now, I need to use this cookie in the next calls, and I know Guzzle can save the cookie for me and send it automatically (or not) in the next call using a "CookieJar" or "SessionCookieJar", I have tried to use it but I do not see the cookie in the 'jar', here is what I have done:

$cookieJar = new SessionCookieJar('SESSION_STORAGE', true);

        $client = new Client([
          'cookies' => $cookieJar
        ]);

        $response = $client->request ....

but, when I get the cookie back from the POST, I can see it only by using:

$newCookies = $response->getHeader('set-cookie');

and its not in the cookieJar, so it won't send it in the next call.. what am I missing here?

Thank you!

Delude answered 31/7, 2017 at 3:7 Comment(2)
docs.guzzlephp.org/en/stable/quickstart.html#cookies. You need to set the jar for the login request in order for it to collect the cookieParyavi
Thanks for your answer, I have tried to set it in the request and in the new client init nothing works (I've read the manual many times), can you please add some code?Delude
B
26

As per the the documentation here, ['cookies' => true] indicates the use of a shared cookie jar for all requests, while ['cookies' => $jar] indicated the use of a specific cookie jar ($jar) for use with client's requests/ responses. So you would need to use either:

$client = new Client(array(
    'cookies' => true
));


$response = $client->request('POST', 'http://myapi.com/login', [
    'timeout' => 30,
    'form_params' => [
        'email' => $request->get('email'),
        'password' => $request->get('password'),
    ]
]);

// and using the same client

$response = $client->request('GET', 'http://myapi.com/next-url');

// or elsewhere ...

$client = new Client(array(
    'cookies' => true
));

$response = $client->request('GET', 'http://myapi.com/next-url');

or

$jar = new CookieJar;

$client = new Client(array(
    'cookies' => $jar
));


$response = $client->request('POST', 'http://myapi.com/login', [
    'timeout' => 30,
    'form_params' => [
        'email' => $request->get('email'),
        'password' => $request->get('password'),
    ]
]);

// and using the same client

$response = $client->request('GET', 'http://myapi.com/next-url');

// or elsewhere ...

$client = new Client(array(
    'cookies' => $jar // the same $jar as above
));

$response = $client->request('GET', 'http://myapi.com/another-url');
Bayadere answered 7/9, 2017 at 16:46 Comment(0)
B
1
//post form params
$response = $client->post(route('login'), [
    'form_params' => [
        'login' => 'login',
        'password' => 'password',
    ],
    'headers' => [
        'Accept' => 'text/html,application/xhtml+xm…ml;q=0.9,image/webp,*/*;q=0.8',
        'Content-Type' => 'application/x-www-form-urlencoded',
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
    ]
]);

//get cookie from header in response
$cookie = $response->getHeaderLine('Set-Cookie');

//get page with cookie
$client->get('/products', [
    'headers' => [
        'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
        'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding' => 'gzip, deflate, br',
        'accept-language' => 'ru',
        'cookie' => $cookie, //this cookie data
    ],
    'debug' => false
]);
Blaubok answered 28/1, 2022 at 15:29 Comment(1)
Hi and thanks for the answer. It would be great if you could explain what your code does. It would help the community much more that way to learn from you!Impassioned

© 2022 - 2025 — McMap. All rights reserved.