Skip to content

Commit

Permalink
minor #19968 [HttpClient] Fix how cookies are defined and sent (javie…
Browse files Browse the repository at this point in the history
…reguiluz)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[HttpClient] Fix how cookies are defined and sent

Fix #19947.

Commits
-------

0643f16 [HttpClient] Fix how cookies are defined and sent
  • Loading branch information
OskarStark committed Jun 17, 2024
2 parents 2071829 + 0643f16 commit 4511703
Showing 1 changed file with 9 additions and 4 deletions.
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

0 comments on commit 4511703

Please sign in to comment.