CodeIgniter Passing POST data from RestClient to RestServer API
Asked Answered
D

5

17

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.

Decalcify answered 10/7, 2014 at 17:1 Comment(1)
Try with set format with only json like $this->rest->format('json');Tissue
F
7

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);
}
Florez answered 15/6, 2015 at 6:43 Comment(0)
P
7

i figure out when and empty 3rd perameter is passed(or when you remove 3rd param) then it works.

    //Not working
    $user = $this->rest->post('employer/postNewProject', $param, 'json');
    //when i change to this ,it's working
     $user = $this->rest->post('employer/postNewProject', $param.'');
     //or 
    $user = $this->rest->post('employer/postNewProject', $param);

if some one has a better solution so that i can award bounty.

Preece answered 12/6, 2015 at 13:27 Comment(0)
F
7

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);
}
Florez answered 15/6, 2015 at 6:43 Comment(0)
A
4

make sure you receive data, use

 echo '<pre> all data received: '.print_r($_REQUEST,1).'</pre>';

make sure is from post

echo '<pre> post data: '.print_r($_POST,1).'</pre>';
Aitken answered 18/6, 2015 at 22:23 Comment(0)
M
1

Content type application/x-www-form-urlencoded used for post method

 $param = array(
            'id' => $this->input->post('id'), // works fine here
            'name' => $this->input->post('name')
            );

 $user = $this->rest->post('employer/postNewProject', $param, 'application/x-www-form-urlencoded');

Content type json for post method RestServer API will be changed for post

public function postNewProject_post()
{  
   $entity = file_get_contents('php://input', 'r');
   print_r($entity);
}
Matchmark answered 5/1, 2016 at 11:22 Comment(0)
F
-2

On your RestServer API, it won't have access to POST data, the POST data from RestClient API is passed into RestServer API via $param array, and RestServer API receives that array. In RestServer API you should be able to access those values via the $param array assuming the RestServer API functions consume the $param array and passes it thru.

Falla answered 10/7, 2014 at 17:12 Comment(4)
Can you show me how to access the $param on the RestServer ?Decalcify
Actually in CodeIgnitor, you should be able to access the post vars via $this->post('id'); Therefore, I am led to believe that maybe the rest url being requested might be ill formatted. Do you have access to the RestAPI server log to see what url is being requested?Falla
By RestAPI server log you mean codeigniter logs right? I did check it, and there is no errors about my api.Decalcify
Are you running apache or nginx as a web server? Check the access-logs... so that you can see what url's are being accessed on the server... that way we can take a look at the API requests coming in and scrutinize the url formattingFalla

© 2022 - 2024 — McMap. All rights reserved.