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

Body is always empty #135

Closed
bpedroza opened this issue Oct 25, 2019 · 2 comments
Closed

Body is always empty #135

bpedroza opened this issue Oct 25, 2019 · 2 comments

Comments

@bpedroza
Copy link

Version: 1.2.1

Steps to recreate:
$response = new \Nyholm\Psr7\Response(203, [], 'hello world'); var_dump($response->getBody()->getContents()); var_dump((string)$response);

Expectation:
Both var_dump calls dump "hello world"

Actual result:
Both var_dump calls dump "" (an empty string)

@Zegnat
Copy link
Collaborator

Zegnat commented Oct 26, 2019

For that last one, I would expect a fatal error (Object of class Nyholm\Psr7\Response could not be converted to string). Only Stream objects can be cast to a string. And when I use (string)$response->getBody() it works for me (I get “hello world”.)

That one also describes one of our unit tests: testCanConstructWithBody. So I am not surprised that it works. Can you confirm that it works when you do not try to cast the entire Response object?

The first one is … just not how getContents() works. The method is not for reading the entirety of a Stream, casting to a string does that. The PSR-7 definition is very clear that it should only return “the remaining contents”. This is a bit of a tricky issue. In the case of the body Stream, there is none, because the cursor is already at the end of the message. Sadly there is no clear definition of where the cursor should end up when a Stream first gets created.

For more reading, there is #99 which will also link you to my research about the problem on the PHP-FIG mailing list, as well as a link to a proposal for fixing it.

@bpedroza
Copy link
Author

Thanks for the response. I'll close as the other issue is already addressing my problem here.

gisostallenberg added a commit to gisostallenberg/php-graphql-client that referenced this issue Nov 17, 2021
PHP FIG PSR-7 describes getContents as "Returns the *remaining* contents in a string", while the interface description mentions "[...] including serialization of the entire stream to a string.".
For some implementations of PSR-7 the stream is not rewinded and thus getContents turns out to be an empty string in some cases. Also see https://github.com/php-fig/http-message/blob/efd67d1dc14a7ef4fc4e518e7dee91c271d524e4/src/StreamInterface.php#L136-L143 and Nyholm/psr7#135 (comment).
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

2 participants