Symfony 5.* + Guzzle : cookies persistence

  cookies, guzzle, php, symfony

I’m currently developping an application in Symfony using Guzzle. I succesfully created a service where I make my requests (one request to get a list of enterprises, another to get user infos, …) but I have issues regarding cookies in Guzzle. I’ve got to say I’m a newbie regarding API so I’m learning as I read the documentation but found nothing interesting for the moment. I’ve tried everything found on the internet so far but didn’t get the result I wanted.

When I make a request, I get a property "Set-Cookie" in my response that I need to put in my next requests. The "Set-Cookie" property is something like "EfficySession=XX-XXXXX~XXXXXXXX-XXXXXXXX; path=/crm/; expires=Wed, 13 Oct 2021 23:22:14 GMT; HttpOnly".

So far this is where I am :

I create my client in the construct in order to be able to use the same client in every method :

public function __construct()
        $this->client = new Client(["base_uri" => "BASE_URI", "allow_redirect" => true]);

And this is my test request to try setting my cookies right :

public function testFunction()
        $json = json_encode([
                "@name" => "api",
                "@func" => [
                        "@name" => "currentuserfullname"

        $jar = new CookieJar();

        $headers =  [

            'X-Efficy-ApiKey' => $this->apiKey,
            'X-Efficy-Logoff' => 'false',
            'Content-Type' => 'application/json'


        $options = ["headers" => $headers, "body" => $json, "cookies" => $jar];

        $response = $this->client->request('GET', 'json', $options);

        $cookieParser = new SetCookie();
        $cookie = $cookieParser->fromString($response->getHeader("Set-Cookie")[0]);

        return json_decode($response->getBody()->getContents())[0]->{'@func'}[0];

But my cookies doesn’t seem to be stored since I always get the property "Set-Cookie" in my response’s headers… I think I’ve tried everything, from using SessionCookieJar to using CookieJar but nothing seems to be working.

Maybe I don’t understand things the right way but as I said above, I’m just starting with API so sorry if you see big mistakes in my code.

Source: Symfony Questions