Skip to content

Commit 0f6b353

Browse files
[HttpClient][BrowserKit] Add examples to set cookies in the request
1 parent 729fb78 commit 0f6b353

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

components/browser_kit.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ into the client constructor::
288288
$client = new Client([], null, $cookieJar);
289289
// ...
290290

291+
Sending Cookies
292+
~~~~~~~~~~~~~~~
293+
294+
By setting the ``Cookie`` header value, you are able to send cookies with your
295+
request thanks to the ``serverParameters`` argument of the
296+
:method:`Symfony\\Component\\BrowserKit\\AbstractBrowser::request` method::
297+
298+
$client->request('GET', '/', [], [], [
299+
'HTTP_COOKIE' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
300+
301+
// you're also able to pass a string instead
302+
'HTTP_COOKIE' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
303+
]);
304+
305+
.. note::
306+
307+
All HTTP headers set with the ``serverParameters`` argument must be
308+
prefixed by ``HTTP_``.
309+
291310
History
292311
-------
293312

http_client.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,21 @@ You can either handle cookies yourself using the ``Cookie`` HTTP header or use
686686
the :doc:`BrowserKit component </components/browser_kit>` which provides this
687687
feature and integrates seamlessly with the HttpClient component.
688688

689+
However, you're able to send cookies in your request. This is how you can do
690+
so using :class:`Symfony\\Contracts\\HttpClient\\HttpClient`::
691+
692+
use Symfony\Component\HttpClient\HttpClient;
693+
use Symfony\Component\HttpFoundation\Cookie;
694+
695+
$client = HttpClient::create([
696+
'headers' => [
697+
'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
698+
699+
// you're also able to pass a string instead
700+
'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
701+
],
702+
]);
703+
689704
Redirects
690705
~~~~~~~~~
691706

0 commit comments

Comments
 (0)