Skip to content
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

Closed
deadacute opened this issue Jul 27, 2015 · 8 comments
Closed

$request->getBody() returns empty stream #1386

deadacute opened this issue Jul 27, 2015 · 8 comments

Comments

@deadacute
Copy link

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

@JoeBengalen
Copy link
Contributor

Maybe something with the pointer already being at the end after parsing the body?

@deadacute
Copy link
Author

_rewind()_ing didn't help so I don't think that's the problem

@geggleto
Copy link
Member

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.
slim uses getParsedBody internally which is why if its a parsable item the body is left empty.
Rewinding does not help because PHP is dumping the content as it reads it as its basically a byte buffer.

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()

@deadacute
Copy link
Author

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.

@geggleto
Copy link
Member

@geggleto
Copy link
Member

//cc @akrabat

opengeek added a commit to opengeek/Slim that referenced this issue Jul 29, 2015
This is according to PSR-7 specifications

Partially addresses slimphp#1386
opengeek added a commit to opengeek/Slim that referenced this issue Jul 29, 2015
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
opengeek added a commit to opengeek/Slim that referenced this issue Aug 2, 2015
This is according to PSR-7 specifications

Partially addresses slimphp#1386
opengeek added a commit to opengeek/Slim that referenced this issue Aug 2, 2015
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
akrabat added a commit that referenced this issue Aug 2, 2015
@akrabat akrabat closed this as completed Aug 2, 2015
@akrabat
Copy link
Member

akrabat commented Aug 2, 2015

@Outfl3sh The tip of the 3.x branch should fix this issue for you.

@deadacute
Copy link
Author

Great, thanks for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants