2

I have this problem with inserting data to MySQL which require POST method by using postman.

this function data_post() inserts data from database, but when I am trying to insert data raw data with postman

{"id":"2","name":"ropen","password":"pamela005"}

I am having this error on postman:

405 Method not Allowed

This is my Controller Users.php

public function data_post(){
    $params = [
        'id' => 1,
        'name' => 'John Doe',
        'password' => 'test'
    ];
    $resp = $this->user_model->data($params);
    $this->set_response($resp, REST_Controller::HTTP_CREATED);    
}

Model User_model.php

public function data($data){   
      $this->db->insert('user',$data);
   }
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Arwin Chua
  • 73
  • 2
  • 7

1 Answers1

0

Hope this will help you :

You have to get post data using $this->post() first , should be like this :

Note : if your id column is autoincrement there is no need to add id in $params

public function data_post()
{
    $id = $this->post('id');
    $name = $this->post('name');
    $password = $this->post('password');

    $params = array('id' => $id,'name' => $name,'password' => $password);
    $resp = $this->user_model->data($params);
    $this->set_response($resp, REST_Controller::HTTP_CREATED);    
} 

For more : https://github.com/chriskacerguis/codeigniter-restserver#handling-requests

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 1
    do update and delete in the same way, just try if u face any problem then post a new question – Pradeep Jul 19 '18 at 07:19
  • r u using postman to update the data, if yes just get data using `$this->post() ` and then update it – Pradeep Jul 19 '18 at 07:54
  • Im having problems with update and delete method I dont know why, It returns Data has been deleted or updated but when I look into my database nothing happens – Arwin Chua Jul 19 '18 at 10:19
  • public function update_users($id,$data) { $this->db->where('id',$id)->update('user',$data); return array('status' => 200,'message' => 'Data has been updated.'); } public function delete_users($id) { $this->db->where('id',$id)->delete('user'); return array('status' => 200,'message' => 'Data has been deleted.'); } – Arwin Chua Jul 19 '18 at 10:31
  • for now, yes I am using postman. After this code works I am going to use android and .net c# later – Arwin Chua Jul 19 '18 at 10:37
  • what column name and id you r sending with post request? – Pradeep Jul 19 '18 at 10:37
  • name and password – Arwin Chua Jul 19 '18 at 10:40
  • do a post request and your delete method should be like this : `public function delete_post() { $id = $this->post('id'); $this->db->where('id',$id)->delete('user'); return array('status' => 200,'message' => 'Data has been deleted.'); } ` – Pradeep Jul 19 '18 at 10:48
  • it works thanks. but why _delete() method does not work? – Arwin Chua Jul 19 '18 at 10:59
  • sorry I only have 13 reputation I'll upvote it later if I get 15+ reputation – Arwin Chua Jul 19 '18 at 13:07