Welcome Guest, Not a member yet? Register   Sign In
Phil Sturgeon's REST Client: help posting to REST server
#1

[eluser]cgeisel[/eluser]
Hi all,

I'm new to CI and using Phil Sturgeon's REST Client & Server libraries, but I'm having a really hard time getting the client to POST correctly.

I'm using his REST Server code exactly as written. The service is located here:
http://test.christophergeisel.com/restserver/

Here's my REST Client:
Code:
class Newtest extends Controller
{
    function index()
    {
      $this->load->library('rest', array(
        'server' => 'http://test.christophergeisel.com/restserver/'
      ));
      
      $method = 'post';
      //$method = 'get';
      $format = 'application/json';
      $params = array('name' => 'Phil');

      $this->rest->format($format);

      if(in_array($method, array('put', 'post')))
      {
        $uri = 'index.php/example_api/user';
        $result = $this->rest->{$method}($uri, $params);
      }
      elseif(in_array($method, array('get', 'delete')))
      {
        $uri = 'index.php/example_api/user/id/1';
        $result = $this->rest->{$method}($uri);
      }
      else {
        $result = "bad method, bub.";
      }
      $this->rest->debug();
      print_r($result);
    }


}

When I set the method to 'get', the uri is set to 'index.php/example_api/user/id/1'. That works fine!

If I set the method to 'post', the uri is set to 'index.php/example_api/user', and I send an array as the parameters, in this case: array('name' => 'Phil');

That gets me a response, but the 'name' parameter isn't set. Here's the code from the REST Server library, in example_api.php:
Code:
function user_post()
    {  
        //$this->some_model->updateUser( $this->get('id') );
        $message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');

        $this->response($message, 200); // 200 being the HTTP response code
    }

Can anyone give me a hand here? I don't understand why my parameter isn't being received by the server, and I'm stumped. Shouldn't $this->post('name') get the value of 'name' from my parameter array? I'm confused. I also took a look at the file test.php that Phil includes with his REST Client code. He unserializes the parameters before sending them, but otherwise I think his technique is the same.

Any help is much appreciated. I've been staring at this code for so long, I'm probably missing something obvious.
#2

[eluser]cgeisel[/eluser]
Victory! It's what I suspect is a bug in the example_api.php REST_Controller. This method:
Code:
$this->post()

... doesn't seem to be an alias for $this->input->post() after all. When I changed my example_api.php code to:

Code:
function user_post()
{  
    //$this->some_model->updateUser( $this->get('id') );
    $message = array('id' => $this->get('id'), 'name' => $this->input->post('name'), 'email' => $this->input->post('email'), 'message' => 'ADDED!');
        
    $this->response($message, 200); // 200 being the HTTP response code    
}

It started receiving the POSTed parameters correctly. Whew! I post my solution here in case anyone else runs into a similar problem.

I've also opened an issue in the REST Client github site. I'll post back here if the author has any feedback.
#3

[eluser]lerva[/eluser]
THANK YOU! I had this exactly same problem. Let's hope that Phil fixes his examples. Those are great libraries by the way!
#4

[eluser]alexanderm[/eluser]
I'm having a really hard time with the client. My server is responding nicely to get requests, but I need to run other requests against it. Duh, right? How else can I see if my REST service works?

How can I get my client to send "CodeIgniter-esque" urls? All I'm doing is:

Code:
function index()
        {
                $this->load->library('rest', array(
                        'server' => 'http://<ip addr>/dev/alpha/index.php/api/'
                ));
                
                $user = $this->rest->get('user', array('id' => 11), 'json');

                $this->rest->debug();
        }

$this->rest->debug() says that my request is:

http://<ip addr>/dev/alpha/index.php/api/user?id=11

and my server responds with a 404.

How can I make the REST client send the get as .../user/id/11 ???


This all wouldn't be so bad if I could get my server to allow either query strings or CI-esque URLs, but I set $config['enable_query_strings'] = TRUE; in config.php, and my server still won't respond to query strings. It returns a 404. The REST framework never sends the request to my controller.

Please help.
#5

[eluser]alexanderm[/eluser]
[quote author="cgeisel" date="1275645734"]Victory! It's what I suspect is a bug in the example_api.php REST_Controller. This method:
Code:
$this->post()

... doesn't seem to be an alias for $this->input->post() after all. When I changed my example_api.php code to:

Code:
function user_post()
{  
    //$this->some_model->updateUser( $this->get('id') );
    $message = array('id' => $this->get('id'), 'name' => $this->input->post('name'), 'email' => $this->input->post('email'), 'message' => 'ADDED!');
        
    $this->response($message, 200); // 200 being the HTTP response code    
}

It started receiving the POSTed parameters correctly. Whew! I post my solution here in case anyone else runs into a similar problem.

I've also opened an issue in the REST Client github site. I'll post back here if the author has any feedback.[/quote]

I have to second this post after spending some time scratching my head. $this->post('name'); does not return the name. $this->input->post('name'); returns the name.

Still love the REST_Controller.
#6

[eluser]lerva[/eluser]
[quote author="alexanderm" date="1277496536"]I'm having a really hard time with the client. My server is responding nicely to get requests, but I need to run other requests against it. Duh, right? How else can I see if my REST service works?

How can I get my client to send "CodeIgniter-esque" urls? All I'm doing is:

Code:
function index()
        {
                $this->load->library('rest', array(
                        'server' => 'http://<ip addr>/dev/alpha/index.php/api/'
                ));
                
                $user = $this->rest->get('user', array('id' => 11), 'json');

                $this->rest->debug();
        }

$this->rest->debug() says that my request is:

http://<ip addr>/dev/alpha/index.php/api/user?id=11

and my server responds with a 404.

How can I make the REST client send the get as .../user/id/11 ???


This all wouldn't be so bad if I could get my server to allow either query strings or CI-esque URLs, but I set $config['enable_query_strings'] = TRUE; in config.php, and my server still won't respond to query strings. It returns a 404. The REST framework never sends the request to my controller.

Please help.[/quote]

Drop the array and just use:

$user = $this->rest->get('user/id/11', array(), 'json');
#7

[eluser]alexanderm[/eluser]
Thank you, Ierva! That worked.
#8

[eluser]xtcsonik[/eluser]
Thanks. Have it pretty much memorized but haven't been able to figure this one out.




Theme © iAndrew 2016 - Forum software by © MyBB