CodeIgniter Forums
Codeigniter 3.1.8 JSON Post fails - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter 3.1.8 JSON Post fails (/showthread.php?tid=70675)



Codeigniter 3.1.8 JSON Post fails - pigfox - 05-12-2018

Hello Community,
I am attempting a JSON POST request to a controller.
The header is:
Code:
[{"key":"Content-Type","value":"application/json"}]
I am trying a minimal payload:
Code:
{"user":1}
According to multiple write ups I have tried the following:

Code:
public function authorize(){
   echo 'Line 37';//Works perfectly
   print_r($this->input->raw_input_stream);//NULL
   print_r(json_decode($this->input->raw_input_stream));//NULL
   print_r(json_decode(file_get_contents('php://input')));//NULL
   print_r(json_decode(trim(file_get_contents('php://input')), true));//NULL
   print_r(json_decode(file_get_contents("php://in‌​put"), true));//file_get_contents(): Invalid php:// URL specified
}


I am using Postman to make the request.
Any clue?


RE: Codeigniter 3.1.8 JSON Post fails - jreklund - 05-13-2018

It looks okay to me...
Make sure that file_get_contents are activated by trying it on another resource.  ->raw_input_stream utilizes that function.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Postman extends CI_Controller {
    
    public function 
index()
    {
 
       if$this->input->is_ajax_request() )
 
       {
            
$json $this->input->raw_input_stream;
            
$json json_decode($json);
            
/**
            $json now looks like
                stdClass Object
                (
                    [user] => 1
                )
                
            Access it like
            echo $json->user;
            **/
            
            // Process json...
            
            // Data to send back
            
$data = array('success' => 'true');
            
            
// Send back json
            
$this->output
                    
->set_content_type('application/json')
                    ->
set_output(json_encode($data));
 
       }
    }


Postman settings:
Content-Type: application/json
X-Requested-With: xmlhttprequest


RE: Codeigniter 3.1.8 JSON Post fails - pigfox - 05-13-2018

According to my host: I can confirm that file_get_contents and allow_url_fopen are enabled.
Then I tried your suggestion.


Code:
public function authorize(){
  if($this->input->is_ajax_request()) {
      echo "This is an Ajax request\n";
      $json = json_decode($this->input->raw_input_stream);
      var_dump($json);//NULL
      var_dump(json_decode(trim(file_get_contents('php://input')), true));//NULL
  }
}

I am also using the headers you suggested, see screen shot.
Any other ideas?


RE: Codeigniter 3.1.8 JSON Post fails - jreklund - 05-13-2018

Have you tried grabbing another source/website with file_get_contents() to confirm it's actually activated?
Have you tried making a POST without https? In case your Cross-Origin Resource Sharing (CORS) are set wrong. Don't know if this actually apply to Postman thought.
Do you have the latest Codeigniter?
Do you have the latest PHP version? (7.1 or 7.2)


RE: Codeigniter 3.1.8 JSON Post fails - pigfox - 05-13-2018

(05-13-2018, 12:39 PM)jreklund Wrote: Have you tried grabbing another source/website with file_get_contents() to confirm it's actually activated?
Have you tried making a POST without https? In case your Cross-Origin Resource Sharing (CORS) are set wrong. Don't know if this actually apply to Postman thought.
Do you have the latest Codeigniter?
Do you have the latest PHP version? (7.1 or 7.2)

My PHP version is .7.0.28
Codeigniter version is 3.1.8
The following code snippet works.

PHP Code:
<?
    var_dump(json_decode(trim(file_get_contents('php://input')), true));
?>

Posting {"user":1} gives me:
array(1) {
  ["user"]=>
  int(1)
}