How to handle json data sent as HTTP message to cakephp application?

If an HTTP message is sent to me, where the body of the HTTP request is only a UTF8 encoded string, how can I access this data in my cakephp controller? It looks like $ this-> params contains only the following:

{
    "pass":[],
    "named":[],
    "controller":"users",
    "action":"checkin",
    "plugin":null,
    "url":{
        "ext":"json",
        "url":"users\/checkin.json"
    },
    "form":[],
    "isAjax":false
}

Published data look something like this:

{
    "sessionkey":"somecrazykey",
    "longitude":"-111.12345",
    "latitude":"33.12345",
    "reqtype":"checkin",
    "location":"the mall",
    "public":"true"
}
+5
source share
2 answers

if ($ this-> RequestHandler-> requestWith ('json')) {if (function_exists ('json_decode')) {$ jsonData = json_decode (utf8_encode (trim (file_get_contents ('php: // input'))), true ); }

    if(!is_null($jsonData) and $jsonData !== false) {
        $this->data = $jsonData;
    }
}

, , . https://trac.cakephp.org/ticket/6125. , , .

-

+7

:

$data = $this->request->input ( 'json_decode', true) ;
+3

All Articles