Skip to content

Commit 4f790b8

Browse files
[BrowserKit] Add response management to BrowserKit documentation
1 parent 7bb008f commit 4f790b8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/browser_kit.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,30 @@ dedicated web crawler or scraper such as `Goutte`_::
343343
'.table-list-header-toggle a:nth-child(1)'
344344
)->text());
345345

346+
Dealing with HTTP responses
347+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
348+
349+
When using the BrowserKit component, you may need to deal with responses of
350+
the requests you made. To do so, you simply have to make a request, then call
351+
the ``getResponse()`` method of the ``HttpBrowser`` object. This method
352+
retrieves the last response the browser received::
353+
354+
$browser = new HttpBrowser(HttpClient::create());
355+
356+
$browser->request('GET', 'https://foo.com');
357+
$response = $browser->getResponse();
358+
359+
If you're making requests that result in a JSON response, you
360+
maye use the ``toArray()`` method of the response to directly get the
361+
array representation of the JSON string you received. This avoids an
362+
extra-call to ``json_decode()``::
363+
364+
$browser = new HttpBrowser(HttpClient::create());
365+
366+
$browser->request('GET', 'https://api.foo.com');
367+
$decodedResponse = $browser->getResponse()->toArray();
368+
// $decodedResponse is an array of the decoded JSON.
369+
346370
Learn more
347371
----------
348372

0 commit comments

Comments
 (0)