-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Prevent empty request body stream in HttplugClient and Psr18Client #59691
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
[HttpClient] Prevent empty request body stream in HttplugClient and Psr18Client #59691
Conversation
This prevents adding `Content-Length` and `Transfer-Encoding` headers when sending a request with an empty body using CurlHttpClient
@nicolas-grekas Thanks. I have a related issue: We have a proxy implementation that forwards the received requests to an upstream server using PSR-7. The upstream server was responding with a 5xx status code response because we were sending a When the ServerRequest object is created in PsrHttpFactory, the body stream wraps the This will prevent sending body content related headers in this case. WDYT? @@ -104,7 +104,7 @@ final class Psr18Client implements ClientInterface, RequestFactoryInterface, Str
$options = [
'headers' => $headers,
- 'body' => 0 === $size ? '' : static fn (int $size) => $body->read($size),
+ 'body' => 0 >= $size && false === array_search($request->getMethod(), ['PATCH', 'POST', 'PUT'], true) ? $body->getContents() : static fn (int $size) => $body->read($size),
];
if ('1.0' === $request->getProtocolVersion()) { |
I'd say this is on you when you create the request object... |
I don't control how the request is created. The PSR7 request object is created using the I have a middleware to convert the body to string when the method is not expected to have a body, but I think this could be fixed in Symfony side.
|
I don't see how Symfony can do anything about it. GET requests can have a body so that the logic you suggest based on methods might work for you, but it won't for everybody. You're still in control of creating the HttpFoundation's Request object so you can do something about this. |
I'll try to move my logic to the HttpFoundation request creation. Thanks for the conversation. |
I've detected that several headers related to the body content are sent to upstream HTTP servers while sending any GET request with an empty body since I updated my app to Symfony 7.2
This PR prevents sending these headers when the body is known to be empty.