How can I get HTTP request body data in codeigniter?

I am having trouble getting JSON-encoded POSTed data to my CI controller from my IOS client client. I believe my problem is the same as mentioned here . But I can’t find the documentation for the query object on the CodeIgniter website. How to view / analyze data in HTTP request body?

$ this-> request-> body

Thanks!

+5
source share
5 answers

I managed to get this job using

$jsonArray = json_decode(file_get_contents('php://input'),true); 

The above will read the request body from the input request and then decodes the JSON into an associative array.

I would still be interested in refactoring this code if CI has a wrapper for reading data from the input stream data stream, since I am above. Pretty new to this frame.

+18
source

Try using $this->input->raw_input_stream

+2
source

The request object mentioned in this post belongs to a package named codeigniter-restserver. You can find more information here .

The accepted answer is working, but CodeIgniter provides $this->input->raw_input_stream which gives you exactly what you need. So, to write things in the CodeIgniter way, you should use:

 $jsonArray = json_decode($this->input->raw_input_stream, true); 

And you will notice that the source code raw_input_stream also uses the file_get_contents('php://input') method file_get_contents('php://input') .

0
source

If you are working with version codeigniter> = 4, you can try this:

 $requestBody = json_decode($this->request->getBody()); $user = $requestBody->user; 

If you are working with version 3, you can try

 $this->input->post('user', TRUE); 
0
source

From the CodeIgniter documentation for CI 2.1, I see that the Input class provides the following call

  $this->input->post(NULL, TRUE); // returns all POST items with XSS filter 

This way you can verify that the request body is filtered by XSS

-3
source

Source: https://habr.com/ru/post/1397252/


All Articles