Cphalcon: getRawBody different after instantiate new Request twice

Created on 6 Jul 2016  路  5Comments  路  Source: phalcon/cphalcon

Hi,

I believe this could be related to the issue:
https://github.com/phalcon/cphalcon/issues/10694

Class PhalconHttpReques

When I am instantiating twice new Request the second time the method getRawBody() returns empty value in a PUT Request.
E.G:

$request = new Request();
$data = $request->getRawBody();
syslog(LOG_INFO,' DATA 1: '.var_export($data,true));

$request = new Request();
$data = $request->getRawBody();
syslog(LOG_INFO,' DATA 2: '.var_export($data,true));

This is happening in version:
phalcon => enabled
Author => Phalcon Team and contributors
Version => 2.0.7
Build Date => Sep 4 2015 16:08:04
Powered by Zephir => Version 0.7.1b

All 5 comments

@javierfrancia Could you please check 2.0.13?

Why would you instantiate Request at first place? You can use the one from memory (IoC services.php -> DI). Then, what is the point of instantiating it two times in a row, during same app runtime?

HTTP is stateless protocol, i.e. every new request from a client is literally - new request.

I guess that second time you instantiate the Request object, it will be a fresh instance (that's why we use shared services in IoC container) and thus will be missing any data from HTTP client request.

Thanks for your comment @stamster. I totally agree with that. It is weird that when you do a new Request() yo are able to get the details of a request.

I am quite amateur with Phalcon but following the documentation: https://docs.phalconphp.com/en/latest/reference/request.html. In order to get the details of the request you instantiate it.

It looks like it is an intended behaviour of this Object. In my example, I am making a request to my service.

Thanks @sergeyklay I will have a look at 2.0.13 but will then ask to change phalcon version on a production environment from 2.0.7 to 2.0.13. This will need to be prioritised in the pipeline.

Yes, but that example is if you only use Request as a stand-alone component.
In full MVC app (or in Micro app too) you have a benefit of each component being registered in IoC container (usually services.php) during bootstrap of the application.
So you just access Request component as:
if ($this->request->getClientAddress() === '212.216.129.50') //do something

If you instantiate it again manually it will obviously work, i.e. it will re-read raw request from PHP superglobals, but on second instance it will empty buffers.

Thanks @stamster for the explanation.

Was this page helpful?
0 / 5 - 0 ratings