Reputation: 2957
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.
Upvotes: 17
Views: 19271
Reputation: 282
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);
}
Upvotes: 1
Reputation:
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>';
Upvotes: 4
Reputation: 2375
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.
Upvotes: 7
Reputation: 22532
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);
}
Upvotes: 7
Reputation: 36
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.
Upvotes: -2