Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update http_client.rst #14174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions 39 http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -883,21 +883,18 @@ In case the response was canceled using ``$response->cancel()``,
Handling Exceptions
~~~~~~~~~~~~~~~~~~~

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

// the response of this request will be a 403 HTTP error
$response = $client->request('GET', 'https://httpbin.org/status/403');
The preferred way to handle this is to call ``$response->getStatusCode()``, cause this
will prevent subsequent ``getHeaders()`` and ``getContent()`` calls from throwing an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will throw all the time (unless false is passed), but that will prevent the destructor from throwing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start with the easy question: I guess that ->toArray() behaves the same as getHeaders() and getContent()? So it should me mentioned too?

Now the hard: So you're saying that getHeaders(true) throws always? But it's not the destructor but some other code part that is issuing the exception (which doesn't make a difference to the user)? So which difference does calling getStatusCode() then make at all?

Sorry, but every time I think I've got it, I realize that I haven't ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, toArray also has the $throw argument
All the methods that have this argument throw accordingly, whether you called getStatusCode() or not.
On destruction, if you didn't call getStatusCode(), an exception will be thrown when the status is >= 300.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I still don't get it :-( Here's the code sample from https://symfony.com/doc/4.4/http_client.html#handling-exceptions

// the response of this request will be a 403 HTTP error
$response = $client->request('GET', 'https://httpbin.org/status/403');

// this code results in a Symfony\Component\HttpClient\Exception\ClientException
// because it doesn't check the status code of the response
$content = $response->getContent();

Could you please show me the full code that "because it doesn't check the status code of the response" refers to?

exception.

// this code results in a Symfony\Component\HttpClient\Exception\ClientException
// because it doesn't check the status code of the response
$content = $response->getContent();

// pass FALSE as the optional argument to not throw an exception and return
// instead the original response content (even if it's an error message)
$content = $response->getContent(false);
Alternatively, if you pass ``false`` as the optional argument to ``getHeaders()``
or ``getContent()``, the response content will be returned and no exception thrown,
no matter which status code it is.

While responses are lazy, their destructor will always wait for headers to come
back. This means that the following request *will* complete; and if e.g. a 404
Expand Down Expand Up @@ -927,16 +924,14 @@ errors, exceptions will notify you when needed. On the other hand, if you write
the error-handling code, you will opt-out from these fallback mechanisms as the
destructor won't have anything remaining to do.

There are three types of exceptions:

* Exceptions implementing the :class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`
are thrown when your code does not handle the status codes in the 300-599 range.

* Exceptions implementing the :class:`Symfony\\Contracts\\HttpClient\\Exception\\TransportExceptionInterface`
are thrown when a lower level issue occurs.
Additionally, if a lower level issue occurs, an exception implementing the
:class:`Symfony\\Contracts\\HttpClient\\Exception\\TransportExceptionInterface`
is thrown.

* Exceptions implementing the :class:`Symfony\\Contracts\\HttpClient\\Exception\\DecodingExceptionInterface`
are thrown when a content-type cannot be decoded to the expected representation.
Finally, if the content-type cannot be decoded to the expected representation, an
exception implementing the
:class:`Symfony\\Contracts\\HttpClient\\Exception\\DecodingExceptionInterface`
is thrown.

Concurrent Requests
-------------------
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.