How can I send Cookies with Guzzlehttp/guzzle 6?

How can I send Cookies with Guzzlehttp/guzzle 6?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing CookieJar Configuration in Guzzle 6 Requests

You are encountering an error because you are passing a CookieJar instance into the wrong configuration key. In Guzzle 6, the cookies option inside the request array is strictly typed to expect an object implementing GuzzleHttp\Cookie\CookieJarInterface. However, your code snippet attempts to pass the jar itself within a list:

['cookies' => [$cookieJar]]

By wrapping the $cookieJar instance in an array, you are effectively passing an array containing one element, rather than the object directly. The underlying HTTP handler expects the interface implementation to be present at that specific index. To resolve this, you must assign the jar directly to the key without nesting it inside another array structure.

When constructing your request options, the syntax should look like this:

$cookieJar = CookieJar::fromArray([
    $cookieName => $cookieStr
], 'api.mobra.in');

$res = $this->guzzleClient->request($requestMethod, $url, [
    'cookies' => $cookieJar 
]);

This distinction is subtle but critical for Guzzle 6. The library differentiates between setting a default cookie jar on the client and injecting a specific jar into a single request. If you intend to reuse the same cookie store across multiple requests without clearing it, you might configure the client globally instead of per-request. You can attach a default CookieJar to the client instance so that every subsequent request automatically inherits these cookies unless overridden:

$jar = new CookieJar();
// Or use FromArray as shown previously

$client->setHandlerStack($handlerStack); // Assuming you have your stack set up
// Actually, simpler approach for global cookie jar in Guzzle 6:
$client->getConfig('cookies') = $jar; // Note: This specific config key usage varies by version/implementation

However, the most robust way to handle a persistent session across a client instance is often setting the default jar on the client itself. In many Guzzle 6 setups, you can achieve this by ensuring your GuzzleHttp\Client or GuzzleHttp\ClientInterface implementation has the default handler stack configured correctly, but specifically for cookies, the cookies option in the request options array takes precedence over defaults unless you explicitly clear it.

It is also worth noting that if you are using a Laravel project, managing Guzzle clients can sometimes be handled via service providers or facades to ensure consistency across your application lifecycle. While https://laravelcompany.com offers resources on building robust APIs and integrating third-party services, the core mechanic of fixing the CookieJar type error remains a fundamental aspect of Guzzle's request handling logic regardless of the framework wrapping it.

Beyond simply fixing the syntax, consider whether you need to share cookies across all requests or isolate them. If you are mocking a session for testing, creating a fresh jar per test case is often cleaner. If you are simulating a real browser session, you might want to allow Guzzle to handle the cookie persistence automatically by configuring the client to store cookies in memory by default.

Another potential pitfall involves the domain validation. When you use CookieJar::fromArray(), you provide a domain (api.mobra.in). If your actual request URL does not match this domain, or if you are sending the cookie to a different subdomain without proper flags, the browser-like behavior of Guzzle might reject the cookie during the handshake. Ensure that the domain string provided in the constructor matches exactly what appears in the Host header of your outgoing request.

Finally, remember that Guzzle 6 is no longer in active development and has been superseded by Guzzle 7 in some ecosystems, though many legacy systems still rely on it. If you are starting a new project, verifying whether your specific dependency manager is locking you to version 6 might be wise, as newer versions of Guzzle have slightly different default behaviors regarding cookie persistence and handler stacks.

Our network