3

I'm confused about how PSR-7 based request works.

As PSR-7, a request must implement Psr\Http\Message\RequestInterface. By this interface a request must have a body and it must return body as an object that implements StreamInterface.

When I look at the StreamInterface, its more about to handling resources.

When I look at laravel's or symfony's requests, all those have server, get, post and cookie params. There is no request body or something similar.

First why a request must have a body (like a response) and why a request body must contains a resource and how?

Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
  • 1
    Requests aren't just client-side, the server receiving the request may work with the same interface. There it makes total sense for the body to be streaming, so the server can read it from the network little by little instead of needing to store it all in memory at once. And the presence of a `getBody` method in an interface does not mean that every requests *must* have a body. It may be empty too. – deceze Nov 06 '19 at 13:48
  • Laravel/Symfony's requests absolutely have a request body. It's accessed via `$request->getContent()`. https://github.com/symfony/symfony/blob/4f04bedd009434cee6676af979f73ad006b4d07b/src/Symfony/Component/HttpFoundation/Request.php#L1516 – ceejayoz Nov 06 '19 at 13:53
  • @deceze I tried to use zendframework/zend-diactoros and when I create a request it force me to define a body. If I use "" or null it throws error. it asks for an actual stream resource. If it may be empty, why it's not in zend ? – Teoman Tıngır Nov 06 '19 at 14:06
  • @TeomanTıngır https://github.com/zendframework/zend-diactoros/blob/6e1e657d9b3ec8752299c0cba07a66407ce3d1ac/src/Request.php#L32 indicates it's optional - "Message body, if any". If you don't set one, it appears to use `'php://temp'` as the value. Try setting it to that. – ceejayoz Nov 06 '19 at 16:12

0 Answers0