Http POST with Content-type: application/json
Request post attribute is currently filled with data from $_POST array. Would it be possible to set it like this
Nette\Utils\Json::decode(file_get_contents('php://input'), true); when method is POST and Content-type is application/json?
I don't think it should be on by default. But an option, maybe?
:+1: on built-in support for application/json. I'm however not sure what's the best way to do it.
@fprochazka Why do you think it shouldn't be on by default? Or maybe other solution would be to add a new method to Request class - getJSON. It would be more general and usable for other request methods (PUT,...).
I wouldn't like having it in $post of Http\Request. But I guess it wouldn't break anything.. right?
Maybe having it auto decode and set as $post of Application\Request in router layer, would be better?
Adding getJson() to Http\Request is an "ok but conservative" way to do it. The question is whether we want to be smart about it.
One option would be to add general getBody() which would return getRawBody() parsed according to current Content-Type (with default support for JSON, XML and FormData)
@JanTvrdik And how should we add support for custom types? Don't forget that Http\Request should not be a service in the future.
@enumag Remember that Http\Request is only a dummy value object. The actual data parsing would be done in Http\RequestFactory.
Does anybody feel like writing a prototype?
I have made a getJSON method draft: novotnyj/http@0e68551bc18b22c4e587180483cc3eed436511b4