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
Support reading the RAW POST body #1443
Conversation
Using file_get_contents("php://input") rather than $HTTP_RAW_POST_DATA
*/ | ||
public function getPostBody() | ||
{ | ||
return file_get_contents("php://input"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://php.net/manual/en/wrappers.php.php
Note: A stream opened with php://input can only be read once; the stream does not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.
Your current code won't work properly.
CHANGELOG line is also needed. |
@@ -186,6 +186,15 @@ public function getPost($name,$defaultValue=null) | |||
{ | |||
return isset($_POST[$name]) ? $_POST[$name] : $defaultValue; | |||
} | |||
|
|||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, try to keep consistency. You have space indented code and there are no trailing dots (look at the other methods of CHttpRequest).
Btw, this is a BC breaking PR. Suppose we have the following code: public function actionIndex()
{
// imagine that this line located somewhere inside CHttpRequest and has been appeared in 1.1.13
CVarDumper::dump(file_get_contents("php://input"));
// imagine that this line was added by an end user before 1.1.13 were released
CVarDumper::dump(file_get_contents("php://input"));
} We decided to perform PUT request after upgrading our project to 1.1.13:
Result (
|
I don't see why would someone call |
@mdomba, use case: assume you have to retrieve raw POST body in two independent places in your project. As i pointed above you can't just call With implemented My suggestion on how should be implemented this feature (just a draft to show my idea—not tested code): public function getRawBody()
{
static $rawBody;
if($rawBody===null)
$rawBody=file_get_contents('php://input');
return $rawBody;
} Of course we should mention this change in UPGRADE file. I'm voting for merging this enhancement, but only when it becomes better. |
I propose |
@creocoder, not only POST request can have body, as i pointed above. It's very strange to invoke |
@resurtm Yes, you are right! :) |
@resurtm I see your point... but how many times will someone need that. In your example if someone needs this just once... than we are storing all the data and occupying the memory. POST data can be really big |
This change is for the use case of RESTful APIs where either XML or JSON data is put directly in the request body. @resurtm I understand the issue you brought up about calling file_get_contents("php://input") further upstream. I tried to look up some ways of perhaps intercepting that stream wrapper or replacing it simply for "php://input" but it doesn't seem possible. So the plan is to rename the method to getRawBody(), align the code to match the code conventions and implement the method with a static store. |
I think we already have it. It's named |
ahhhh, thanks @samdark, this does look like it would work, but the method is marked as protected. |
Will handle this one by adding better named @itamar82 you can speed it up a bit by updating pull-request with this solution. |
Thanks for ideas and code. |
Added method getPostBody() to CHttpRequest in order to support reading the entire POST body contents.
The method is using file_get_contents("php://input") rather than $HTTP_RAW_POST_DATA for efficiency.