Skip to content

Commit fb86deb

Browse files
Update http_client.rst
I started from scratch after symfony#14174 * The most important change is that the old text "your code is expected to handle it" didn't make it clear *how* people are supposed to handle it. * I omitted the note that a `Symfony\Component\HttpClient\Exception\ClientException` is thrown. Is this important? Or is the existing `Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface` enough?
1 parent 501f371 commit fb86deb

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

http_client.rst

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -891,20 +891,21 @@ Handling Exceptions
891891
~~~~~~~~~~~~~~~~~~~
892892

893893
When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
894-
4xx or 5xx) your code is expected to handle it. If you don't do that, the
895-
``getHeaders()``, ``getContent()`` and ``toArray()`` methods throw an appropriate exception, all of
896-
which implement the :class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`::
894+
4xx or 5xx), the ``getHeaders()``, ``getContent()`` and ``toArray()`` methods
895+
throw an appropriate exception, all of which implement the
896+
:class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`.
897897

898-
// the response of this request will be a 403 HTTP error
899-
$response = $client->request('GET', 'https://httpbin.org/status/403');
898+
You can prevent the exception from being thrown by either:
900899

901-
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
902-
// because it doesn't check the status code of the response
903-
$content = $response->getContent();
900+
* Checking for the exact status code::
901+
902+
$response = $client->request('GET', 'https://httpbin.org/status/403');
903+
if ('403' === $response->getStatusCode()) {
904+
$response->getContent();
905+
}
904906

905-
// pass FALSE as the optional argument to not throw an exception and return
906-
// instead the original response content (even if it's an error message)
907-
$content = $response->getContent(false);
907+
* Or passing ``false`` as the optional argument to the methods, e.g.
908+
``$response->getHeaders(false);``
908909

909910
While responses are lazy, their destructor will always wait for headers to come
910911
back. This means that the following request *will* complete; and if e.g. a 404

0 commit comments

Comments
 (0)