http icon indicating copy to clipboard operation
http copied to clipboard

Http POST with Content-type: application/json

Open novotnyj opened this issue 10 years ago • 9 comments

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?

novotnyj avatar Feb 26 '15 10:02 novotnyj

I don't think it should be on by default. But an option, maybe?

fprochazka avatar Feb 26 '15 10:02 fprochazka

:+1: on built-in support for application/json. I'm however not sure what's the best way to do it.

JanTvrdik avatar Feb 26 '15 10:02 JanTvrdik

@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,...).

novotnyj avatar Feb 26 '15 11:02 novotnyj

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?

fprochazka avatar Feb 26 '15 11:02 fprochazka

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 avatar Feb 26 '15 11:02 JanTvrdik

@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 avatar Feb 26 '15 11:02 enumag

@enumag Remember that Http\Request is only a dummy value object. The actual data parsing would be done in Http\RequestFactory.

JanTvrdik avatar Feb 26 '15 11:02 JanTvrdik

Does anybody feel like writing a prototype?

JanTvrdik avatar Feb 26 '15 11:02 JanTvrdik

I have made a getJSON method draft: novotnyj/http@0e68551bc18b22c4e587180483cc3eed436511b4

novotnyj avatar Feb 26 '15 12:02 novotnyj