简体   繁体   中英

How to get Json input in CakePhp Restful API using PUT

I am able to view and delete the data by passing ID in the URl in format of:

/apis/view/id.json

using:

public function view($id) {
        $api = $this->Api->findById($id);
        $this->set(array(
            'api' => $api,
            '_serialize' => array('api')
        ));
    }

Similarly I want to implement add and edit , where I can pass data in Json format in the HTTP body and store/edit it in the database.

I couldn't follow the this solution: CakePHP API PUT with JSON input

I couldn't understand how to use

$data = $this->request->input('json_decode');

to achieve it.

Add can simply be used as given in documentation by appending .json to it. The URL at which you post the data will become /apis.json . This will automatically access the add() method.

Assuming you pass json values email and password in such format: {"email":"abc@def.com","password":"123456"}

public function add(){

     $data=$this->request->input('json_decode', true ); //$data stores the json 
//input. Remember, if you do not add 'true', it will not store in array format.

     $data = $this->Api->findByEmailAndPassword($data['email'],$data['password']);
//simple check to see if posted values match values in model "Api". 
         if($data) {$this->set(array(
                          'data' => $data,
              '_serialize' => array('data')));}
        else{ $this->set(array(
            'data' => "sorry",
            '_serialize' => array('data')));}

      }//  The last if else is to check if $data is true, ie. if value matches,
      // it will send the input values back in JSON response. If the email pass
      // is not found, it will return "Sorry" in json format only.

Hope that answers your question! Put is also very similar, except it will check if the data exists, if it doesn't it will create or else it will modify existing data. If you have any further doubts don't hesitate to ask :)

As explained in the linked documentation , CakeRequest::input() reads the raw input data, and optionally passes it through a decoding function.

So $this->request->input('json_decode') gives you the decoded JSON input, and in case it's formatting follows the Cake conventions , you can simply pass it to one of Model save methods.

Here's a very basic (untested) example:

public function add()
{
    if($this->request->is('put'))
    {
        $data = $this->request->input('json_decode', true);

        $api = $this->Api->save($data);
        $validationErrors => $this->Api->validationErrors;

        $this->set(array
        (
            'api' => $api,
            'validationErrors' => $validationErrors,
            '_serialize' => array('api', 'validationErrors')
        ));
    }
}

This will try to save the data and return the save result as well as possible validation errors.

In case the formatting of the input data doesn't follow the Cake conventions, you'll have to transform it accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM