Skip to content

hope $this->request->get() can return body data #13418

Closed
@itbdw

Description

Expected and Actual Behavior

I wanna use $this->request->get() to get body and $_REQUEST, and the getPut method does not parse json request.

I hacked in my code, hope this can be done within Phalcon framework.

$di->set('request', function () {

    class PSRequest extends \Phalcon\Http\Request {

        public function getPut(
            $name = null,
            $filters = null,
            $defaultValue = null,
            $notAllowEmpty = false,
            $noRecursive = false
        ) {
            if (strpos($this->getContentType(), 'json') !== false) {
                $put = $this->getJsonRawBody(true);
                if (gettype($put) != 'array') {
                    $put = [];
                }
                $this->_putCache = $put;
            }

            return parent::getPut($name, $filters, $defaultValue, $notAllowEmpty,
                $noRecursive);
        }

        /**
         * get data fro Body and $_REQUEST
         *
         * @param null $name
         * @param null $filters
         * @param null $defaultValue
         * @param bool $notAllowEmpty
         * @param bool $noRecursive
         * @return mixed
         */
        public function get($name = null, $filters = null, $defaultValue = null, $notAllowEmpty = false, $noRecursive = false) {
            $res = null;
            
            if (strpos($this->getContentType(), 'json') !== false) {
                $res = self::getPut($name, $filters, null, $notAllowEmpty, $noRecursive);
            }
            
            if ($res === null) {
                $res = parent::get($name, $filters, null, $notAllowEmpty, $noRecursive);
            }
            
            if ($res === null) {
                $res = $defaultValue;
            }

            return $res;
        }
    }

    return new \PSRequest();
});

Activity

Jurigag

Jurigag commented on Jul 1, 2018

@Jurigag
Contributor

I don't like this. But i think that getPut method should detect if request was made as json or not and use getJsonRawBody if needed instead of getRawBody.

itbdw

itbdw commented on Jul 2, 2018

@itbdw
Author

I understand your concern. Keep simple and clear.

But as we know, there are many front framework or tools (like Axios) is sending json body by default. By adding lines above, retrieving data become easy again.

Indeed, I agree your view on getPut method. And it is very easy to do that. Judge the content type before parse_str the $this->getRawBody();

var put;
let put = this->_putCache;
if typeof put != "array" {
    if (strpos($this->getContentType(), 'json') !== false) {
        let put = this->getJsonRawBody();
        if (typeof put != "array") {
             let put = [];
       }
       let this->_putCache = put;
    } else {
        let put = [];
        parse_str(this->getRawBody(), put);
        let this->_putCache = put;
    }
}

Jurigag

Jurigag commented on Jul 2, 2018

@Jurigag
Contributor

Yea, but there is already getPut method which is working unfortunately on raw body, we need to make it I think detect if it's json, if then parse it properly.

itbdw

itbdw commented on Jul 3, 2018

@itbdw
Author

What about getPost method? It may receive json body too when content-type is json. That's why I wrote a new getJson method to deal with this case.

So, these method should also be upgraded to compatible with json format?

getPut, getPost, get
hasPut, hasPost, has

=====

I think the name of getPut method is not equal with what it does.

The doc says "Gets a variable from put request", but it get the request body php://input in fact, regardless of the request method PUT, POST or what ever.

====

So, I have changed my hack code and just expand the getPut method and get method to satisfy my need.

changed the title hope $this->request->get() can parse data from json request body hope $this->request->get() can return body data on Jul 3, 2018
sergeyklay

sergeyklay commented on Jul 3, 2018

@sergeyklay
Contributor

Fixed in the 3.4.x branch. Feel free to open a new issue if the problem appears again. Thank you for contributing.

added this to the 3.4.1 milestone on Jul 3, 2018
self-assigned this
on Jul 3, 2018
Jurigag

Jurigag commented on Jul 21, 2018

@Jurigag
Contributor

getPut() method obviously works regardless of request method. There is no $_PUT in php, PUT is just type of method, but in php speaking PUT is body of request.

Also you can send post data as well with PUT method.

1 remaining item

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    hope $this->request->get() can return body data · Issue #13418 · phalcon/cphalcon