Skip to content

Commit 0a241c1

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [HttpClient][BrowserKit] Add examples to set cookies in the request
2 parents 705f9ed + 01aaf48 commit 0a241c1

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

components/browser_kit.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,27 @@ into the client constructor::
273273
$client = new Client([], null, $cookieJar);
274274
// ...
275275

276+
.. _component-browserkit-sending-cookies:
277+
278+
Sending Cookies
279+
~~~~~~~~~~~~~~~
280+
281+
Requests can include cookies. To do so, use the ``serverParameters`` argument of
282+
the :method:`Symfony\\Component\\BrowserKit\\AbstractBrowser::request` method
283+
to set the ``Cookie`` header value::
284+
285+
$client->request('GET', '/', [], [], [
286+
'HTTP_COOKIE' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
287+
288+
// you can also pass the cookie contents as a string
289+
'HTTP_COOKIE' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
290+
]);
291+
292+
.. note::
293+
294+
All HTTP headers set with the ``serverParameters`` argument must be
295+
prefixed by ``HTTP_``.
296+
276297
History
277298
-------
278299

http_client.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,21 @@ requires a stateful storage (because responses can update cookies and they must
675675
be used for subsequent requests). That's why this component doesn't handle
676676
cookies automatically.
677677

678-
You can either handle cookies yourself using the ``Cookie`` HTTP header or use
679-
the :doc:`BrowserKit component </components/browser_kit>` which provides this
680-
feature and integrates seamlessly with the HttpClient component.
678+
You can either :ref:`send cookies with the BrowserKit component <component-browserkit-sending-cookies>`,
679+
which integrates seamlessly with the HttpClient component, or manually setting
680+
the ``Cookie`` HTTP header as follows::
681+
682+
use Symfony\Component\HttpClient\HttpClient;
683+
use Symfony\Component\HttpFoundation\Cookie;
684+
685+
$client = HttpClient::create([
686+
'headers' => [
687+
'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
688+
689+
// you can also pass the cookie contents as a string
690+
'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
691+
],
692+
]);
681693

682694
Redirects
683695
~~~~~~~~~

0 commit comments

Comments
 (0)