Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Passing POST data from RestClient to RestServer API

I've spent my whole day trying to figure out this problem. Posting this issue here is my last hope. I hope someone can help to continue working on my first job.

So, POST works fine when directly passing data from my views to the RestServer directly. However, RESTServer API is not able to find POSTs data sent from the RestClient.

Here are the snippets:

RestClient API:

        $ver = 'v1';
        $config = array('server'          => base_url()."v1",
                        'api_key'         => 'xxxxxx',
                        'api_name'        => 'X-API-KEY',
                        'http_user'       => 'admin',
                        'http_pass'       => 'xxxxx',
                        'http_auth'       => 'basic',
                );

        $this->rest->initialize($config);
        $this->rest->format('application/json');
        $param = array(
                'id' => $this->input->post('id'), // works fine here
                'name' => $this->input->post('name')
                );
        $user = $this->rest->post('employer/postNewProject', $param, 'json');


        //if (isset($user->errors)) show_404('admin');
        $this->rest->debug();

RestServer API

class Employer extends REST_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->lang->load("application");
        $this->load->library("users/auth"); 
        Datamapper::add_model_path( array( APPPATH."modules" ) );
    }

    public function postNewProject_post()
    {  
        // I tired $this->post() and $this->input->post() but both not finding the data
        $message = array("id" => $this->post("id"), "name" => $this->input->post("name"));
        $this->response($message, 200); // 200 being the HTTP response code
    }
}

Results:

Response when using $this->post('id');
{"id":false}

Response when using $this->post();
{"id":[]}

Note: I've replaced the POSTS requests with hardcoded data, and still the RestServer is not able to recieve the data from my RestClient.

If you need me to provide anything else, please ask.

Thank you in advance.

like image 345
Sobiaholic Avatar asked Jul 10 '14 17:07

Sobiaholic


1 Answers

Use this method to send Rest client data to rest server using post method. Read comment on every line

// initialize you setting 
     $config = array('server' => base_url() . "v1",
            'api_key' => 'xxxxxx',
            'api_name' => 'X-API-KEY',
            'http_user' => 'admin',
            'http_pass' => 'xxxxx',
            'http_auth' => 'basic',
        );
        $this->rest->initialize($config);
// Set method of sending data

        $method = 'post';
// create your param data

        $param = array(
            'id' => $this->input->post('id'), // works fine here
            'name' => $this->input->post('name')
        );
// url where you want to send your param data.

        $uri = 'employer/postNewProject';
        // set format you sending data
        $this->rest->format('application/json');

// send your param data to given url using this

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

//code for your controller employer/postNewProject

Controller

function postNewProject()
{
    $data=array(
        'id' => $this->post('id'),
        'name' => $this->post('name')
        );
      print_r($data);
}
like image 200
Saty Avatar answered Sep 23 '22 11:09

Saty