Skip to content

[HttpClient] Fix how cookies are defined and sent #19968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -693,17 +693,21 @@ cookies automatically.

You can either :ref:`send cookies with the BrowserKit component <component-browserkit-sending-cookies>`,
which integrates seamlessly with the HttpClient component, or manually setting
the ``Cookie`` HTTP header as follows::
`the Cookie HTTP request header`_ as follows::

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Cookie;

$client = HttpClient::create([
'headers' => [
'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
// set one cookie as a name=value pair
'Cookie' => 'flavor=chocolate',

// you can also pass the cookie contents as a string
'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
// you can set multiple cookies at once separating them with a ;
'Cookie' => 'flavor=chocolate; size=medium',

// if needed, encode the cookie value to ensure that it contains valid characters
'Cookie' => sprintf("%s=%s", 'foo', rawurlencode('...')),
],
]);

Expand Down Expand Up @@ -2089,3 +2093,4 @@ test it in a real application::
.. _`EventSource`: https://www.w3.org/TR/eventsource/#eventsource
.. _`idempotent method`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods
.. _`SSRF`: https://portswigger.net/web-security/ssrf
.. _`the Cookie HTTP request header`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
Loading