Gawran
Gawran

Reputation: 358

Codeigniter Rest Server fail to output longer JSON

Does anybody know why $this-response($somearray, 200); Can't output a long JSON? (Or long arrays) For example I can output DB result if I limit query to last 10 rows but if I limit it to 100 JSON gets trimed and in my client app I get 'unexpected end of input'. If I change above statement in simple echo json_encode($somearray); It works flawlessly.

I am sorry. I need to clarify my question. I am using https://github.com/chriskacerguis/codeigniter-restserver

function transits_get()
{
    $sessionId = $this->input->get_request_header('Sessionid', TRUE);
    $transits = $this->Transit_model->get_transits( $sessionId );

    if($transits)
    {
        //$this->output->set_content_type('application/json');
        //$this->output->set_output(json_encode($transits)); //works
        //echo json_encode($transits); //works
        $this->response($transits, 200); // 200 being the HTTP response code //Does not work

    }

    else
    {
        $this->response(array('error' => 'There is no transit records in the database!'), 404);
    }
}

Upvotes: 2

Views: 2228

Answers (3)

Muhammad Fahad
Muhammad Fahad

Reputation: 1412

In libraries folder, line no. 267 of REST_Controller.php file, comment the following code. it's working.

header('Content-Length: ' . strlen($output));

Upvotes: 0

VaMoose
VaMoose

Reputation: 196

This works for me, and I have lots of JSON data (+- 250 records):

$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));

As found in this article, you can do the following:

  1. Open REST_Controller.php

  2. Locate and delete the following code (at the end of the response function):

    if ( ! $this->_zlib_oc && ! $CFG->item('compress_output')) {
        header('Content-Length: ' . strlen($output));
    }
    

Upvotes: 0

Gawran
Gawran

Reputation: 358

Alright, I've found what was the problem. I simply commented out the code in REST_controller that sets content length. Now it works but I am still wondering why strlen($output) is not as same length as output JSON for larger JSON files and it is ok for short ones.

// If zlib.output_compression is enabled it will compress the output,
    // but it will not modify the content-length header to compensate for
    // the reduction, causing the browser to hang waiting for more data.
    // We'll just skip content-length in those cases.
    if ( ! $this->_zlib_oc && ! $this->config->item('compress_output')) {
        //header('Content-Length: ' . strlen($output));
    }

Upvotes: 1

Related Questions