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

Error with PUT parameters in CHttpRequest ( method getRestParams() ) #3457

Closed
pedrocalgaro opened this issue Jul 18, 2014 · 2 comments
Closed
Milestone

Comments

@pedrocalgaro
Copy link

Hi everyone,

I'm having a problem when I trying to get parameters from a PUT request and I believe is is a bug of the framework.
When the method getRestParams() is called, it tries to parse the rawBody.
The method getRawBody() returns the value of file_get_contents('php://input')

The function file_get_contents('php://input') returns the value:

{"foo": "foo content", "bar": "bar content"}

The function getRestParams() tries to parse this content calling the function mb_parse_str

mb_parse_str($this->getRawBody(), $result);

But this returns:

Array
(
    [{"foo": "foo content", "bar": "bar content"}] => 
)

So I changed the line: (https://github.com/yiisoft/yii/blob/master/framework/web/CHttpRequest.php#L290)

mb_parse_str($this->getRawBody(), $result);

To:

$result = json_decode($this->getRawBody(), true);

And after that, it works like a charm.

You could look at it?

@pedrocalgaro pedrocalgaro changed the title Erro with PUT parâmeters in CHttpRequest ( method getRestParams() ) Error with PUT parâmeters in CHttpRequest ( method getRestParams() ) Jul 18, 2014
@pedrocalgaro pedrocalgaro changed the title Error with PUT parâmeters in CHttpRequest ( method getRestParams() ) Error with PUT parameters in CHttpRequest ( method getRestParams() ) Jul 18, 2014
@cebe
Copy link
Member

cebe commented Jul 18, 2014

Yii does not support JSON requests by default, to enable json input you have to extend CHttpRequest and override getRestParams(). In yii 2.0 we already have support for this. marking this as a feature request.

@cebe cebe added this to the 1.1.17 milestone Jul 18, 2014
@pedrocalgaro
Copy link
Author

Ok, thanks @cebe

Sibilino added a commit to Sibilino/yii that referenced this issue Feb 27, 2015
… JSON in REST method bodies.

Added getContentType()
Added jsonAsString
getRestParams can now use CJSON::decode
@samdark samdark closed this as completed Nov 17, 2015
samdark added a commit that referenced this issue Jan 8, 2016
Enh #3457: CHttpRequest able to decode JSON REST parameters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants