HTTPClient throws an exception when the response status code is within 3xx range #62774
Replies: 7 comments
-
|
This happens only when you don't check the status code beforehand, isn't it? That's the expected behavior. You can, but you have to follow a failsafe pattern. |
Beta Was this translation helpful? Give feedback.
-
|
Oh, and you need also to pass false to getHeaders() / getContent() if you want to deal with this. |
Beta Was this translation helpful? Give feedback.
-
There is no specific failsafe pattern to follow. An endpoint is contractually bound to the response it returns. When we execute an endpoint, we have to check the status code of the response, regardless of the situation. A request that is expected contractually to answer with a 307 is not safe if it answers with a 200. Why would the HTTP client consider 3xx response as less safe than 2xx ones? It knows nothing about the endpoint contract, it can't make such decision. What is the reason behind this design decision? Now, about the problem: when we execute a request to an endpoint that returns 3xx responses, the client throws an exception, regardless of retrieving the headers or the content. We don't even get the chance to retrieve them. Or the status code. That's the point. The client just throws. So, please, it is neither answered nor solved. |
Beta Was this translation helpful? Give feedback.
-
It does so only if you don't check the status code. Show me some reproducer I will tell you how to fix it, that's the best way forward I think. |
Beta Was this translation helpful? Give feedback.
-
I don't understand. We never receive a response since the request execution throws. How could check the response status code is there is no response to begin with? This is what the documentation says by the way, so the behaviour that we are observing is consistent:
Why is the client working in such a way? What is the rational behind not supporting 3xx responses at all? |
Beta Was this translation helpful? Give feedback.
-
Yes you do: just get the result from the call to |
Beta Was this translation helpful? Give feedback.
-
|
It is not explained in the doc, except if I missed something. And the failsafe argument is fallacious : an endpoint that is expected to return 3xx should not fail when it does actually return a 3xx. The client is not failsafe: it actually induces failure; and it is not failsafe when it should be - typically in this case when the response is a 2xx, it should fail. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Symfony version(s) affected
6.4
Description
Whenever we use HTTPClient to execute a request without following redirects, and the response status code is within the 3xx range, the client throws an exception.
This is documented (https://symfony.com/doc/current/http_client.html#redirects) but I still believe this is a bug from an HTTP perspective: a request is a success as long as a response was succesfully returned; and the 3xx range is neither the client error range (4xx), nor a server error range (5xx).
In other words, redirections are not errors, by any mean, from the point of view of HTTP specification. There is no reason why a response with a status code within the 3xx range and the proper
Locationheader is considered as not valid. TheLocationheader presence is the only thing that is made mandatory by the HTTP specification.How to reproduce
Execute a request to an endpoint that returns a 3xx response and a
Locationheader, by configuring the client to not follow redirects.Possible Solution
No response
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions