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

new Response with body is not applied to stream #17

Closed
designermonkey opened this issue Apr 12, 2023 · 3 comments
Closed

new Response with body is not applied to stream #17

designermonkey opened this issue Apr 12, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@designermonkey
Copy link

Description
If I instantiate a Response: $response = new Response(200, [], 'my value'); when I look into the body stream (string) $response->getBody();, there is no string 'my value'.

@designermonkey designermonkey added the bug Something isn't working label Apr 12, 2023
@designermonkey
Copy link
Author

MessageTrait::registerStream expects a stream or stream handle, but ResponseTrait::init is passing the stream content as the handle.

@designermonkey
Copy link
Author

TextResponse uses init as $this->init($code, $reasonPhrase, $headers, $this->createBody($text), $protocol); which would be better for this issue also.

@devanych
Copy link
Member

@designermonkey

Hi. This is not a bug. Laminas response constructor works the same way:
https://github.com/laminas/laminas-diactoros/blob/2.26.x/src/Response.php#L121

  1. You can use the response classes from the httpsoft/http-response package.
  2. But if you want to use a HttpSoft\Message\Response class, then you can use the stream factory for this:
use HttpSoft\Message\Response;
use HttpSoft\Message\StreamFactory;

$streamFactory = new StreamFactory();
$stream = $streamFactory->createStream('my value');

$response = new Response(200, [], $stream);
// Or
$response = (new Response())
    ->withBody($stream)
;

@devanych devanych closed this as completed May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants