Magento 2: getPost and getPostValue methods on the request object

getPostValue() is written in

lib\internal\Magento\Framework\HTTP\PhpEnvironment\Request.php

 /**
     * Retrieve POST parameters
     *
     * @param string $name
     * @param mixed $default
     * @return mixed|ParametersInterface
     */
    public function getPostValue($name = null, $default = null)
    {
        $post = $this->getPost($name, $default);
        if ($post instanceof ParametersInterface) {
            return $post->toArray();
        }
        return $post;
    }

then It get the getPost value from

vendor\zendframework\zend-http\src\Request.php

public function getPost($name = null, $default = null)
    {
        if ($this->postParams === null) {
            $this->postParams = new Parameters();
        }

        if ($name === null) {
            return $this->postParams;
        }

        return $this->postParams->get($name, $default);
    }

Hope you will get atleast some hint.


And yes you can use

$post = $this->getRequest()->getPostValue();

To get post value,,you can also check Contact module to get some hint


Is it OK to assume that these methods are always going to be there for http requests?

Don't think so. They're breaking their own class contract. As you can imagine, that's not OOP-ish at all.

As per the usual recommendation, I would stay away from using anything that's not declared in Magento\Framework\App\RequestInterface because a) you'll make Liskov happy and because b) they'll soon realise the problem and fix it (hopefully), thus breaking your code (or not; but if they do, they're justified: you were not using the API contract, right?).

If they won't fix it, they'll have a very capable API implementation (i.e. Magento\Framework\App\Request\Http) which nobody will really use.

Always stay within the contract!