-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
withBody + getBody results in an empty body #150
Comments
I'm baffled, but when I change my code to do this, it works
I really did not expect php to see direct chaining differently than using a local variable in between. In any case, I stlil think this is very werid behavior and my first code should work too or otherwise give some kind of comprehensible exception why I shouldn't do that.. I still don't understand why the two statements are different |
Every time you call a method on an object, it actually returns a completely new object rather than updating the existing one. So when you call By making sure you redefine the variable $request = $requestFactory->createRequest($method, $url);
$body = json_encode(['test' => 'test']);
$request = $request->withBody($streamFactory->createStream($body));
echo (string)$request->getBody(); // empty string "" (!) Note how I have updated line 4 of your example code to start with This is because PSR-7 objects are defined to be immutable. Meaning (in short) they cannot be changed after being created. To quote from the PSR-7 definition of a Request object:
You can read more about this on the PHP-FIG’s website for PSR-7, under “Why value objects?”. I am closing this issue as there is nothing here for this library to address. But if you have any follow-up questions, feel free to post them as I am happy to answer them :) |
I have the following library code, which is implemented in another Symfony application with httplug and auto discovery.
I've traced this problem to the
MessageTrait
, thewithBody
method (line 126), does this:If I change the code to this, it works as expected:
What I don't understand is that nobody else runs into this problem ;)
My version of nyholm/psr7 is 1.2.1
The text was updated successfully, but these errors were encountered: