Skip to content

Commit 9daacae

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

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

components/browser_kit.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,28 @@ 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+
302+
.. note::
303+
304+
:class:`Symfony\\Component\\HttpFoundation\\Cookie` is a helper class to
305+
help you create cookies. You're also able to pass a simple string as the
306+
value of the ``Cookie`` header.
307+
308+
.. note::
309+
310+
All HTTP headers set with the ``serverParameters`` argument must be
311+
prefixed by ``HTTP_``.
312+
291313
History
292314
-------
293315

http_client.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,24 @@ 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+
]);
700+
701+
.. note::
702+
703+
The :class:`Symfony\\Component\\HttpFoundation\\Cookie` class is a helper to
704+
help you create cookies. You're also able to pass a simple string as the
705+
value of the ``Cookie`` header.
706+
689707
Redirects
690708
~~~~~~~~~
691709

0 commit comments

Comments
 (0)