Skip to content

[HttpClient] Update http_client.rst #14499

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 5 commits into from
Closed
Changes from 3 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
22 changes: 11 additions & 11 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -891,20 +891,20 @@ 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()``, ``getContent()`` and ``toArray()`` methods throw an appropriate exception, all of
which implement the :class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`::
4xx or 5xx), the ``getHeaders()``, ``getContent()`` and ``toArray()`` methods
throw an appropriate exception, all of which implement 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');
You can prevent the exception from being thrown by either:

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

$response = $client->request('GET', 'https://httpbin.org/status/403');
if (403 === $response->getStatusCode()) {
$response->getContent();
Copy link
Member

Choose a reason for hiding this comment

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

This will throw.
Checking the status code disables throwing by the destructor only.
The behavior of the other methods is always predictable and controlled by the $throw argument.
I agree we should find a better way to explain all this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn, I checked for string '403', while in fact it's int...
Please take a look again!

And if you still have some nerves left: :-)
Maybe it would be easier to get across when we could explain the idea behind it! The $throw argument is straightforward. But why did you do the destructor / getStatusCode() thing? What use cases did you have in mind?

And one more: For those 3 methods together (getHeaders(), getContent() and toArray()), do you have some internal (nick) name we could use? Something like "contentFetchers" or so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ping @nicolas-grekas :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas Could you please merge this, before it ages away? :-)

}

// 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);
* Or passing ``false`` as the optional argument to the methods, e.g. ``$response->getHeaders(false);``

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