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.


Messages In This Thread
Phil Sturgeon's REST Client: help posting to REST server - by El Forum - 06-03-2010, 06:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB