Nyholm / psr7

A super lightweight PSR-7 implementation
MIT License
1.16k stars 75 forks source link

Empty body when using psr17factory's createRequest #204

Closed nawedr closed 1 year ago

nawedr commented 1 year ago

Using the psr17factory for creating a request object using this syntax, when we try to get the contents of the body, we get empty string.

$factory = new Psr17Factory();
$req = $factory->createRequest('POST', 'https://nyholm.tech');
$body = $factory->createStream('Foo');
$req = $req->withBody($body);
var_dump($req->getBody()->getContents()); // prints empty string.

The problem is in the createStream method, while creating the stream the file seek goes to the end of the temp file created and when we try to get the contents it starts reading from end, hence the empty string.

nicolas-grekas commented 1 year ago

AFAIK, this behavior is not specified by PSR-7 so the current behavior is also correct. What could help is add a test case for this in https://github.com/php-http/psr7-integration-tests/ and ensuring all implementations agree on the expected behavior first.

nicolas-grekas commented 1 year ago

Note that the cast to string is specified to seek.

nicolas-grekas commented 1 year ago

Still, this should be fixed by #217