Vincent Kituyi
Vincent Kituyi

Reputation: 119

codeigniter status code 400 bad request in index_post() method

I keep on getting this error when performing post and put operations in my codeigniter restful api app.

here is the controller:

public function index_post(){
    if(!$this->input->post('city'))
    {
        $this->response(null,400);
    }
    $id=$this->cities_model->save($this->input->post('city'));
    if(!is_null($id))
    {
        $this->response(array('response'=> $id),200);
    }
    else
    {
        $this->response(array('error'=> 'sorry, data could not be saved...'),400);
    }
}
public function index_put(){
    if(!$this->input->post('city') || !$id)
    {
        $this->response(null,400);
    }
    $update=$this->cities_model->update($id,$this->input->post('city'));
    if(!is_null($update))
    {
        $this->response(array('response' => 'content updated successfully'),200);
    }
    else
    {
        $this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), 400);
    }
}

Upvotes: 1

Views: 3089

Answers (2)

Vincent Kituyi
Vincent Kituyi

Reputation: 119

 class Cities_model extends CI_Model
 {
  public function __construct()
  {
  parent::__construct();

  } 
 public function save($city)
 {
 $this->db->set($this->setCity($city))->insert('cities');
 if($this->db->affected_rows()>0)
 {
 return $this->db->insert_id;
 }
 return null;
 }
  public function update($id,$city)
{
$this->db->set($this->setCity($city))->where('id',$id)->update('cities');
if($this->db->affected_rows()===1)
{
 return true;
 }
 return false;
 }
  private function setCity($city)
  {
   return array('id'=>$city['id'],
         'name'=>$city['name']
         );
    }
    }

Upvotes: 0

jagad89
jagad89

Reputation: 2643

There are few improvements and solution as per below.

public function index_post(){

    // Use validation library, instead of checking just for value.
    $this->load->library('form_validation');
    $this->form_validation->set_rules('city','City','trim|required');

    if($this->form_validation->run() == FALSE)
    {
        // send back list of validation errors.
        $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
    }

    $id=$this->cities_model->save($this->post('city'));
    if(!is_null($id))
    {
        $this->response(array('response'=> $id),REST_Controller::HTTP_OK);
    }
    else
    {
        $this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
    }
}
public function index_put(){
    // for put you need to pass id as parameter 

    // Use validation library, instead of checking just for value.
    $this->load->library('form_validation');
    $this->form_validation->set_rules('id','ID','trim|required|integer');
    $this->form_validation->set_rules('city','City','trim|required');

    if($this->form_validation->run() == FALSE)
    {
        // send back list of validation errors.
        $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
    }

    $update=$this->cities_model->update($this->post('id'),$this->post('city'));
    if(!is_null($update))
    {
        $this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
    }
    else
    {
        $this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
    }
}

Now you need to pass city parameter from postman in POST body. it invoke index_post and you pass city and id parameter in PUT body,it invoke index_put.

Upvotes: 1

Related Questions