Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send POST request to Phil Sturgeon's CodeIgniter RestServer

I'm new to CodeIgniter. I'm using Phil Sturgeon's RestServer and RestClient. I've been trying to make a POST request in my CodeIgniter RestClient controller to update data in my CodeIgniter RestServer, but it never updates data in my database. I think my POST request is not right.

Here is my RestClient POST request in controller:

$result = $this->rest->post('newscontroller/news/format/json', 
          array('news_id' => $news_id,
                'news_title' => $news_title,
                'news_category' => $news_category ),
                'json'); 

if(isset($result->status) && $result->status == 'success')  
{  
        $data['message'] ='News has been updated.';  
        $this->load->view('otherpageview',$data);
}     
else  
{  
        $data['message'] ='Something has gone wrong';  
        $this->load->view('otherpageview',$data);
} 

It seems that $result doesn't get any value, because I did echo the $result->status and it has nothing to display. And I've also have this in this controller's constructor :

// Load the rest client spark
$this->load->spark('restclient/2.1.0');

// Load the library
$this->load->library('rest');

// Run some setup
$this->rest->initialize(array('server' => 'http://api.therestserver.com/index.php/'));

And in the RestServer's controller, which is newscontroller, has this method :

function news_post()
{
    $news=array(
        'news_id' => $this->post('news_id'),
        'news_title' => $this->post('news_title'),
        'news_category' => $this->post('news_category') );

    $result = $this->News_model->UpdateNews($news);  

    if($result === FALSE)  
    {  
        $this->response(array('status' => 'failed'));  
    }  
    else  
    {  
        $this->response(array('status' => 'success'));  
    }
}

With the News_model :

public function UpdateNews($news)
{
    $this->db->where('news_id',$news->news_id);
    $this->db->update('news',$news);        
}

I just don't know where I'm doing wrong, because I still don't understand how the POST request and method work. I've read through the tutorial in Nettuts and search about this, but still.. maybe because of my bad English reading-writing. I hope someone can help me out, any help would be appreciated. Thanks a TON! :)

like image 488
emwiguna Avatar asked Jul 20 '12 14:07

emwiguna


1 Answers

Finally SOLVED this problem! It was my POST request in the RESTClient controller that is wrong. After doing some searching and lots of trying / changing the codes, this code works for me for POST request in my RESTClient controller :

$news_id = 12; //this is the id of the news that you want to edit

$method = 'post';
$params = array('news_id' => $news_id, 
'news_title' => $this->input->post('news_title'), 
'news_category' => $this->input->post('news_category') );
$uri = 'newscontroller/news';

$this->rest->format('application/json');

$result = $this->rest->{$method}($uri, $params);

if(isset($result->status) && $result->status == 'success')  
{  
    $data['message'] ='News has been updated.';  
    $this->load->view('otherpageview',$data);
}     
else  
{  
    $data['message'] ='Something has gone wrong';  
    $this->load->view('otherpageview',$data);
} 

With a lot of help from this reference

I post this if anybody needs an example of the right POST request in RESTClient and RESTServer, because I find it hard to look for an example for POST request in RESTClient-Server*** by Phil Sturgeon.

I'm using :

  • RESTServer (philsturgeon) v.2.6.0
  • RESTClient (philsturgeon) v.2.1.0
  • cURL (philsturgeon) v.1.2.1
  • CodeIgniter v.2.0.3
like image 124
emwiguna Avatar answered Sep 22 '22 13:09

emwiguna