Skip to content

Commit 01aaf48

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: [HttpClient][BrowserKit] Add examples to set cookies in the request
2 parents 1ff389e + bf53b5c commit 01aaf48

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
@@ -674,9 +674,21 @@ requires a stateful storage (because responses can update cookies and they must
674674
be used for subsequent requests). That's why this component doesn't handle
675675
cookies automatically.
676676

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

681693
Redirects
682694
~~~~~~~~~

0 commit comments

Comments
 (0)