I had same situation but now I found solution for this. Now I can put request url without .json and can get Json data in response as well.
In App controller add network response which will handle your response.
use Cake\Network\Response;
After that you need to convert Json input in array, So put this getJsonInput()
function in your AppController
and call it in initialize()
public function getJsonInput() {
$data = file_get_contents("php://input");
$this->data = (isset($data) && $data != '') ? json_decode($data, true) : array();
}
Now in your controller, You have all post data in $this->data
. so you can access all inputs.
Here is an example :
class UsersController extends AppController {
public function index() {
if ($this->request->is('post')) {
//pr($this->data); //here is your all inputs
$this->message = 'success';
$this->status = true;
}
$this->respond();
}
}
Now at the end of your function you need to call respond()
which is defined in AppController
public function respond() {
$this->response->type('json'); // this will convert your response to json
$this->response->body([
'status' => $this->status,
'code' => $this->code,
'responseData' => $this->responseData,
'message'=> $this->message,
'errors'=> $this->errors,
]); // Set your response in body
$this->response->send(); // It will send your response
$this->response->stop(); // At the end stop the response
}
Define all variable in the AppController
as
public $status = false;
public $message = '';
public $responseData = array();
public $code = 200;
public $errors = '';
One more thing to do is:
In the Response.php (/vendor/cakephp/cakephp/src/Network/Response.php)
You need to edit one line at 586
echo $content;
to echo json_encode($content);
in _sendContent()
function.
That's it. Now you can set request url as
domain_name/project_name/users/index
.