0

hello I made a post method with the API, I want to make a response like the following what can?

 {
    "status": 200,
    "error": false,
    "data": [
        {
            "id_kab": "56",
            "id_prov": "1",
            "kd_kab": "CGK10000"
        }
    ]
}

what I did through POSTMAN now results like this

{
     "id_kab": "56",
     "id_prov": "1",
     "kd_kab": "CGK10000"
}

this my code insert to database

     public function submit_post() {
            $data = array(
                'id_kab'        => $this->input->post('id_kab'),
                'id_prov'       => $this->input->post('id_prov'),
                'kd_kab'        => $this->input->post('kd_kab')
            );
            $insert = $this->db->insert('service', $data);
            if ($insert) {
            $arr=array(
            'status'  => 200,
            'message' => 'Success'
            );
             header('Content-Type: application/json');
             echo json_encode($arr,TRUE);
            } else {
                $this->response(array('status' => 'fail', 502));
            }
         }

how to change the response? thank you

2

1 Answer 1

0

From the response given, data should be given as an array, So please try the below code

$output = array();
$data = array();
$data[] = array(
                'id_kab'        => $this->input->post('id_kab'),
                'id_prov'       => $this->input->post('id_prov'),
                'kd_kab'        => $this->input->post('kd_kab')
            );

$output["data"] = $data;
$output["status"] = 200;
$output["error"] = false;
echo json_encode($output); exit;

OutPut is as follows

{
    "status": 200,
    "error": false,
    "data": [{
        "id_kab": "56",
        "id_prov": "1",
        "kd_kab": "CGK10000"
    }]
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.