How do I handle json data sent as an HTTP Post to a cakephp app?

后端 未结 2 1407
日久生厌
日久生厌 2021-01-05 18:10

If I\'m being sent an HTTP Post where the body of the http request is just a UTF8 encoded string, how do I access that data in my cakephp controller? It appears that $this->

相关标签:
2条回答
  • 2021-01-05 18:14
    if($this->RequestHandler->requestedWith('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;
        }
    }
    

    This is a codesnippet which was proposed to be in the core, see https://trac.cakephp.org/ticket/6125. Maybe it's what you're looking for.

    -- Bjorn

    0 讨论(0)
  • 2021-01-05 18:32

    You can use these simplest way :

    $data = $this->request->input ( 'json_decode', true) ;
    
    0 讨论(0)
提交回复
热议问题