Closed
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 commentedon 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 commentedon 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();
Jurigag commentedon 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 commentedon 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?
=====
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.
sergeyklay commentedon 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.Jurigag commentedon 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.
1 remaining item