httpsoft/http-message

new Response with body is not applied to stream

designermonkey opened this issue · 3 comments

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'.

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

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

@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)
;