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

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

Closed
itbdw opened this Issue Jun 28, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@itbdw
Copy link

itbdw commented Jun 28, 2018

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();
});
@Jurigag

This comment has been minimized.

Copy link
Member

Jurigag commented Jul 1, 2018

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

This comment has been minimized.

Copy link
Author

itbdw commented Jul 2, 2018

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

This comment has been minimized.

Copy link
Member

Jurigag commented Jul 2, 2018

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

This comment has been minimized.

Copy link
Author

itbdw commented Jul 3, 2018

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.

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

@sergeyklay

This comment has been minimized.

Copy link
Member

sergeyklay commented Jul 3, 2018

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

@sergeyklay sergeyklay closed this Jul 3, 2018

@sergeyklay sergeyklay added this to the 3.4.1 milestone Jul 3, 2018

@sergeyklay sergeyklay added the Bug - Low label Jul 3, 2018

@sergeyklay sergeyklay self-assigned this Jul 3, 2018

@Jurigag

This comment has been minimized.

Copy link
Member

Jurigag commented Jul 21, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.