diff --git a/components/browser_kit.rst b/components/browser_kit.rst index 0d1f774e845..68ea04929d1 100644 --- a/components/browser_kit.rst +++ b/components/browser_kit.rst @@ -288,6 +288,25 @@ into the client constructor:: $client = new Client([], null, $cookieJar); // ... +Sending Cookies +~~~~~~~~~~~~~~~ + +By setting the ``Cookie`` header value, you are able to send cookies with your +request thanks to the ``serverParameters`` argument of the +:method:`Symfony\\Component\\BrowserKit\\AbstractBrowser::request` method:: + + $client->request('GET', '/', [], [], [ + 'HTTP_COOKIE' => new Cookie('flavor', 'chocolate', strtotime('+1 day')), + + // you're also able to pass a string instead + 'HTTP_COOKIE' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/' + ]); + +.. note:: + + All HTTP headers set with the ``serverParameters`` argument must be + prefixed by ``HTTP_``. + History ------- diff --git a/http_client.rst b/http_client.rst index a94587a966e..763f5fed4bb 100644 --- a/http_client.rst +++ b/http_client.rst @@ -686,6 +686,21 @@ You can either handle cookies yourself using the ``Cookie`` HTTP header or use the :doc:`BrowserKit component ` which provides this feature and integrates seamlessly with the HttpClient component. +However, you're able to send cookies in your request. This is how you can do +so using :class:`Symfony\\Contracts\\HttpClient\\HttpClient`:: + + use Symfony\Component\HttpClient\HttpClient; + use Symfony\Component\HttpFoundation\Cookie; + + $client = HttpClient::create([ + 'headers' => [ + 'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')), + + // you're also able to pass a string instead + 'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/' + ], + ]); + Redirects ~~~~~~~~~