Comments (2)
I'm baffled, but when I change my code to do this, it works
$request = $this->requestFactory->createRequest($method, $this->host.$url)
->withBody($this->streamFactory->createStream($body));
echo (string)$request->getBody(); // now returns the $body as expected!
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
from psr7.
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 $request->withBody(…)
this call returns a new instance of a Request object while the original object $request is unchanged.
By making sure you redefine the variable $request
you can make sure you always have the latest created object ready to use. You can do this by either chaining the method calls (because the final output returned gets assigned to your variable) or by redeclaring the variable like so:
$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 $request =
.
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:
* Requests are considered immutable; all methods that might change state MUST * be implemented such that they retain the internal state of the current * message and return an instance that contains the changed state.
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 :)
from psr7.
Related Issues (20)
- Broken PSR-7 immutability HOT 2
- argument name of withHeader() is inconsistent with PSR-7 definition. HOT 2
- feature request for a non-buffered stream (NonBufferedStream) HOT 3
- Empty body when using psr17factory's createRequest HOT 3
- Filter Uri->withUserInfo() input HOT 2
- Axios get request gives [AxiosError:Network Error] & Error: header name must be a non-empty string
- Response::__toString() HOT 2
- Integration tests errors HOT 2
- Escaping of user info breaks NTLM authentication HOT 9
- "Site not found" for https://tnyholm.se HOT 2
- psr/http-message 版本问题 HOT 4
- PSR/HTTP message incompatibility 2.0 on nyholm/psr7 server HOT 1
- Breaking change from 1.5.1 => 1.6.0 due to fseek on new streams? HOT 3
- https://packagist.org/packages/php-http/message-factory dependand package abandoned HOT 1
- Segmentation Error while using Elasticsearch php's library bulk method HOT 2
- php-http/message-factory HOT 2
- Empty host value not allowed HOT 1
- For what reason message trait contains original header names?
- Support for "Early Hints" / HTTP Code 103 HOT 1
- Request getSize() returns null for php://input stream HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psr7.