From 0643f16f56ba4e751f0c95f31db23781a615da92 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 17 Jun 2024 10:58:20 +0200 Subject: [PATCH] [HttpClient] Fix how cookies are defined and sent --- http_client.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/http_client.rst b/http_client.rst index 8a344e552a2..bc950a382e8 100644 --- a/http_client.rst +++ b/http_client.rst @@ -693,17 +693,21 @@ cookies automatically. You can either :ref:`send cookies with the BrowserKit component `, 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('...')), ], ]); @@ -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