Bart Bergmans
Bart Bergmans

Reputation: 4121

Codeigniter no POST data received

I am doing a POST request to my website but when I debug the $_POST and $this->input->post() and they are both an empty array. I have global_xss_filtering set to FALSE. I also put a debug line at the top of my index.php but that one also returns an empty $_POST array.

Headers of my POST request (received from postcatcher.in):

{
    "content-length": "164",
    "total-route-time": "0",
    "x-request-start": "1431326903012",
    "connect-time": "1",
    "via": "1.1 vegur",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-forwarded-for": "213.124.141.66",
    "x-request-id": "d7f56276-afb5-463c-acf1-9632acc27d9d",
    "accept-encoding": "gzip",
    "user-agent": "Dalvik/2.1.0 (Linux; U; Android 5.1; Nexus 5 Build/LMY47I)",
    "content-type": "application/json;charset=UTF-8",
    "accept": "application/json",
    "connection": "close",
    "host": "postcatcher.in"
}

The POST data I sent:

{
    "phonenumber": "1234356",
    "organisation_id": 0,
    "location_id": 0,
    "lastname": "Bergmans",
    "id": 0,
    "firstname": "Bart",
    "email": "[email protected]",
    "active": false
}

My controller:

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

class Api extends MY_Controller {

        private $client;

        private function connectSoap()
        {
                if($this->client == null) {
                    $this->client = new SoapClient($this->config->item('wsdl_location'));
                }
        }

        public function editUserData($id) {
               error_log(print_r($this->input->post(),true));

               $return = array();
               $return['success'] = 'true';
               $this->outputJson($return);
        }

        private function outputJson($array) 
        {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                            $array
                    ));
        }
}

Upvotes: 1

Views: 2196

Answers (1)

Praveen Dabral
Praveen Dabral

Reputation: 2509

contentType takes application/json, it means the request is sending json data which is not true in your case thats why the data is not recieved.

You have empty $_POST. If your web-server wants see data in json-format you need to read the raw input and then parse it with JSON decode.

You need something like that:

$json = file_get_contents('php://input');
$obj = json_decode($json);

Upvotes: 1

Related Questions