{"id":"3242cbe8f71eb0af","repo":"guzzle/guzzle","slug":"cookie-value-must-be-scalar-or-stringable","errorCode":null,"errorMessage":"Cookie value must be scalar or stringable","messagePattern":"Cookie value must be scalar or stringable","errorType":"validation","errorClass":"\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Cookie/CookieJar.php","lineNumber":65,"sourceCode":"            $this->setCookie($cookie);\n        }\n    }\n\n    /**\n     * Create a new Cookie jar from an associative array and domain.\n     *\n     * @param array  $cookies Cookies to create the jar from\n     * @param string $domain  Domain to set the cookies to\n     */\n    public static function fromArray(\n        #[\\SensitiveParameter]\n        array $cookies,\n        string $domain\n    ): self {\n        $cookieJar = new self();\n        foreach ($cookies as $name => $value) {\n            if (!\\is_scalar($value) && !(\\is_object($value) && \\method_exists($value, '__toString'))) {\n                throw new \\InvalidArgumentException('Cookie value must be scalar or stringable');\n            }\n\n            $cookieJar->setCookie(new SetCookie([\n                'Domain' => $domain,\n                'Name' => (string) $name,\n                'Value' => (string) $value,\n                'Discard' => true,\n            ]));\n        }\n\n        return $cookieJar;\n    }\n\n    /**\n     * Evaluate if this cookie should be persisted to storage\n     * that survives between requests.\n     *\n     * @param SetCookie $cookie              Being evaluated.","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/guzzle/guzzle/blob/9b200fc5805036b331d6031199880dadecae0275/src/Cookie/CookieJar.php#L47-L83","documentation":"Thrown by CookieJar::fromArray() when one of the values in the input cookie array is neither a scalar (int/float/bool/string) nor an object implementing __toString(). The library needs a stringable value because it immediately casts each cookie value to a string to build a SetCookie, so a non-stringable value (array, resource, closure, plain object) cannot be represented as a cookie value.","triggerScenarios":"Calling CookieJar::fromArray(['foo' => ['nested' => 'array'], ...], $domain) or passing an object that does not implement __toString (e.g. a DOMNode, a stdClass without __toString) as a value. Also triggered by resources (fopen handles) or null passed as a value.","commonSituations":"Mapping an API response or DB row straight into fromArray() where a column is a JSON/array type instead of a scalar; passing a Carbon/DateTime object (which is stringable) works but a plain value object without __toString does not; forgetting to serialize a nested structure before storing it as a cookie.","solutions":["Coerce each value to a string before calling fromArray(), e.g. array_map('strval', $cookies) when all values are scalar.","Filter out or skip non-stringable entries: only pass entries where is_scalar($v) || (is_object($v) && method_exists($v, '__toString')).","If you must store structured data in a cookie, json_encode it first so the value is a string.","Implement __toString() on any value object you intend to pass as a cookie value."],"exampleFix":"// before\n$jar = CookieJar::fromArray(['prefs' => ['lang' => 'en']], 'example.com');\n\n// after\n$jar = CookieJar::fromArray(\n    ['prefs' => json_encode(['lang' => 'en'])],\n    'example.com'\n);","handlingStrategy":"validation","validationCode":"foreach ($cookies as $name => $value) {\n    if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {\n        throw new InvalidArgumentException(\"Non-stringable cookie value for '$name'\");\n    }\n}\n$jar = CookieJar::fromArray($cookies, $domain);","typeGuard":"function isStringableValue($value): bool\n{\n    return is_scalar($value)\n        || (is_object($value) && method_exists($value, '__toString'));\n}","tryCatchPattern":"try {\n    $jar = CookieJar::fromArray($cookies, $domain);\n} catch (\\InvalidArgumentException $e) {\n    // log and coerce values: array_map(fn($v) => is_scalar($v) ? (string)$v : '', $cookies)\n}","preventionTips":["Always coerce cookie values to strings (strval/array_map) before fromArray().","json_encode structured data you intend to store in a cookie.","Type-hint the array you pass to fromArray() as array<string,string|int|float|bool>."],"tags":["cookies","input-validation","type-coercion","invalidargumentexception"],"analyzedSha":"9b200fc5805036b331d6031199880dadecae0275","analyzedAt":"2026-08-04T21:24:26.648Z","schemaVersion":2}