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
$request->getBody() returns empty stream #1386
Comments
Maybe something with the pointer already being at the end after parsing the body? |
_rewind()_ing didn't help so I don't think that's the problem |
https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Stream.php#L181 shows that the contents of the stream are returned, thus theres nothing left in the string. Only in a file implementation could you rewind since the data is on disk and not in memory. I do believe this to be a bug in our implementation. Upon Request class initialization the stream should be read in full and stored in a protected attribute, then it can be returned at will via __toString and getContents() |
As @geggleto mentioned according to php.net php://input can be read only once, because right now there is no support for a seek operation. I think there is a really simple solution of just dumping the whole body into a string and then creating a stream from it. Not really a clean solution, but I can't seem to find a better one. |
//cc @akrabat |
This is according to PSR-7 specifications Partially addresses slimphp#1386
This copies the php://input stream to a php://temp stream that can be rewound and read multiple times so that the raw request body is not cannabilised by the first middleware to read it. Also automatically rewinds the request body after reading it in Request::getMethod() on POST so the “first” call to getContents() within the App invocation is not empty. Should resolve slimphp#1386
This is according to PSR-7 specifications Partially addresses slimphp#1386
This copies the php://input stream to a php://temp stream that can be rewound and read multiple times so that the raw request body is not cannabilised by the first middleware to read it. Also automatically rewinds the request body after reading it in Request::getMethod() on POST so the “first” call to getContents() within the App invocation is not empty. Should resolve slimphp#1386
@Outfl3sh The tip of the 3.x branch should fix this issue for you. |
Great, thanks for the fix. |
When I use $request->getParsedBody() everything works fine, but when I use $request->getBody() it returns just empty stream. According to geggleto it looks like the stream gets detached at some point.
demo: source, output
The text was updated successfully, but these errors were encountered: