Home > Article > Backend Development > Solving the problem that the put request in CodeIgniter RestServer cannot obtain parameters_PHP tutorial
I recently encountered a painful problem when using restserver. I found that the parameters obtained by $this->put are all null. After checking, I found that this seems to be a common problem, see the link: https://github.com/chriskacerguis/codeigniter-restserver/issues/362
Let’s take a look at the official explanation first: see http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814
$this->put() Reads in PUT arguments set in the HTTP headers or via cURL.
1, consistent with post, still passing parameters in the body. Write a function in the base class:
public function getPut($key){ return $this->input->input_stream($key); }When the client accesses, just pass the parameters in the body normally and it will be ok. Note that the parameters cannot be obtained through $this->post() at this time, and must be obtained from input_stream. The above function supports fetching multiple fields at the same time, such as:
$data = $this->getPut(array('tel', 'name', 'addr'));In fact, all input functions in CI should support fetching multiple fields at the same time, but Restserver's this->get() post() does not support it.
Supplement: When putting parameters in the body, you can directly use $this->put() to get the corresponding fields. The document says it is in the headers, but it is actually in the body! However, $this->put() does not support multiple fields, so the above function is still meaningful.
$this->delete() also has this problem. The parameters in the headers cannot be read, but the parameters in the body can be read! ! !
2. The parameters are passed in the header and a function is written in the base class:
/** * 获得key对应的header * @param $key * @return mixed */ public function getHeader($key){ return $this->input->get_request_header($key, TRUE); }
ps: The problem that put in restserver cannot obtain parameters has nothing to do with the setting of Content-Type: application/json.