16

से पोस्ट डेटा पास कर रहा है मैंने अपना पूरा दिन इस समस्या को समझने की कोशिश की है। यहां इस मुद्दे को पोस्ट करना मेरी आखिरी उम्मीद है। मुझे आशा है कि कोई मेरी पहली नौकरी पर काम जारी रखने में मदद कर सकता है।कोडइग्निटर रेस्ट क्लाइंट से रेस्टसेवर एपीआई

तो, पोस्ट सीधे ठीक से काम करता है जब सीधे मेरे विचारों से डेटा को रेस्टसेवर तक सीधे पास करता है। हालांकि, RESTServer API RestClient से भेजे गए POSTs डेटा को ढूँढने में सक्षम नहीं है।

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 एपीआई

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 
    } 
} 

परिणाम::

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

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

नोट:

यहाँ के टुकड़े कर रहे हैं मैं हार्डकोडेड डेटा के साथ पोस्ट अनुरोध बदल दिया है, और अभी भी RestServer मेरे RestClient से डेटा प्राप्त करने में सक्षम नहीं है।

यदि आपको मुझे कुछ और प्रदान करने की आवश्यकता है, तो कृपया पूछें।

अग्रिम धन्यवाद।

+0

केवल $ JS- 'rest-> format (' json ') जैसे सेट जेएस के साथ सेट प्रारूप के साथ प्रयास करें; ' – jagad89

उत्तर

7

पोस्ट विधि का उपयोग कर सर्वर को आराम करने के लिए बाकी क्लाइंट डेटा भेजने के लिए इस विधि का उपयोग करें।हर लाइन पर टिप्पणी पढ़ें

// 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); 

// अपने नियंत्रक के लिए कोड employer/postNewProject

नियंत्रक

function postNewProject() 
{ 
    $data=array(
     'id' => $this->post('id'), 
     'name' => $this->post('name') 
     ); 
     print_r($data); 
} 
-2

आपके रेस्टसेवर एपीआई पर, उसके पास POST डेटा तक पहुंच नहीं होगी, RestClient API से POST डेटा $ param सरणी के माध्यम से RestServer API में पास किया गया है, और RestServer API उस सरणी को प्राप्त करता है। RestServer API में आप उन मानों को $ param सरणी के माध्यम से एक्सेस करने में सक्षम होना चाहिए मानते हैं कि RestServer API फ़ंक्शन $ param सरणी का उपभोग करते हैं और इसे पास करते हैं।

+0

क्या आप मुझे दिखा सकते हैं कि RestServer पर' $ param 'का उपयोग कैसे करें? – Sobiaholic

+0

असल में कोडइग्निटर में, आपको $ v-> पोस्ट ('आईडी') के माध्यम से पोस्ट वर्र्स तक पहुंचने में सक्षम होना चाहिए; इसलिए, मुझे विश्वास है कि शायद बाकी यूआरएल का अनुरोध किया जा सकता है। यूआरएल के अनुरोध के लिए क्या आपके पास RestAPI सर्वर लॉग तक पहुंच है? – zbot

+0

RestAPI सर्वर लॉग द्वारा आप का मतलब है कि codeigniter सही है? मैंने इसे जांच लिया, और मेरे एपीआई के बारे में कोई त्रुटि नहीं है। – Sobiaholic

7

मुझे लगता है कि तीसरा पैरामीटर कब खाली और खाली हो जाता है (या जब आप तीसरे परम को हटाते हैं) तो यह काम करता है।

//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); 

यदि किसी के पास एक बेहतर समाधान है ताकि मैं उपहार प्रदान कर सकूं।

4

सुनिश्चित करें कि आप डेटा प्राप्त करते हैं, का उपयोग

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

यकीन है

01 से पोस्ट करें
echo '<pre> post data: '.print_r($_POST,1).'</pre>'; 
0

सामग्री प्रकार आवेदन/x-www फार्म-urlencoded पोस्ट विधि RestServer एपीआई के लिए json पोस्ट विधि के लिए

$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'); 

सामग्री प्रकार इस्तेमाल किया बदल जाएगा पोस्ट

public function postNewProject_post() 
{ 
    $entity = file_get_contents('php://input', 'r'); 
    print_r($entity); 
} 
संबंधित मुद्दे