1

I'm trying to login into a service (Jasper Server API) using the Http Client, but cant get the cookies from the response

This is what I'm doing:

    $response = Http::withOptions(  [ 'proxy' => '' ] )->
                    post('192.168.52.84/jasperserver/rest_v2/login?j_username=user&j_password=password');


    $cookies=$response->cookies;                    
    dd($cookies);

The output being:

GuzzleHttp\Cookie\CookieJar {
  -cookies: []
  -strictMode: false
}

When I do the same Get on the browser I get the cookies fine.

What could be the problem?

  • this might help you https://stackoverflow.com/questions/3220660/local-storage-vs-cookies/36367799#36367799 – John Lobo Jun 03 '21 at 02:52
  • so when you hitting apis then you must set cookie manually in server side .for example return response($content)->withCookie(cookie('name', 'value')); https://laravel.com/docs/5.0/responses – John Lobo Jun 03 '21 at 02:56
  • Hi! That's what I want to do, but I think I'm stuck at step before. I want to set the next request with the cookies of the first, because I need to send all the requests in the same session, but I cannot obtain the first request cookies. – Jorge Gastaldi Jun 03 '21 at 16:10

2 Answers2

3

You can get cookie value by the following code also, get cookie by name and then get the value.

$cookies = $response->cookies()->getCookieByName('YOUR_COOKIE_NAME')->getValue();
Wailan Tirajoh
  • 481
  • 5
  • 17
0

I finally solved it. It was just that when I did:

post('192.168.52.84/jasperserver...')

I didn't specify the scheme:

post('http://192.168.52.84/jasperserver...')

With that, I got the cookies just right.