响应类型正在从JSON更改为HTML,而不进行任何代码更改

I have a legacy application in CakePHP 2.x

It has a method in a Controller that outputs JSON in a structure like this:

{"id":59,"name":"Association of Southeast Asian Nations (ASEAN)","n_grouptags":1}

The controller method used $this->response->type('json'); to set the content-type in the response to application/json; charset=UTF-8. All good.

What I noticed is that if the returned data becomes over a certain length, the content-type is set to text/html; charset=UTF-8 without any changes to the code.

I've included some screenshots below which show this, including the response type.

Small amount of data (content type = application/json - expected):

enter image description here

enter image description here

More data (content type = text/html - unexpected):

enter image description here

enter image description here

In both cases I have checked that the JSON is valid using https://jsonlint.com/

Why is this? Does it depend on the length of the response as to how the browser treats it, or is this a CakePHP problem?

The PHP that is responsible for the output is as follows - but no changes have been made to this between the 2 different outputs given above:

    $this->autoRender = false; // No View (template) is associated with this

    $out = []; // Reset

    // $tags is some data from a model
    foreach ($tags as $k => $v) {
        $n_grouptags = 123; // Actual number comes from a Model 
        $out[] = ['id' => $k, 'name' => $v, 'n_grouptags' => $n_grouptags];
    }

    $this->response->type('json'); // We *want* a JSON response

    echo json_encode($out, JSON_FORCE_OBJECT); // Encode $out (the output) as JSON

Caching within the application is disabled: Configure::write('Cache.disable', true);

Controller actions are not supposed to echo data, even though it might work in some, maybe even most situations. The correct way of outputting data that doesn't stem from a rendered view template, is to configure and return the response object (or a string, but that's not forward compatible with 3.x), or to use serialized views.

The root problem is not the length of the content, but generally outputting data before the response object can send headers, which will cause them to be ignored, this will happen once there's even a single byte being sent before the response emitter comes into play.

It most likely only happens as of a certain length because you're using PHPs output buffering and/or compression (see output_buffering and zlib.output_compression in your php.ini), which will cause the echoed data to be held back until the buffer storage capabilities are being exceeded (in most cases that's usually 4096 bytes), or the buffer is explicitly flushed (which will happen automatically at the end of the script execution).

tl;dr, for a quick fix, configure and return the response:

$this->response->body(json_encode($out, JSON_FORCE_OBJECT));
return $this->response;

See also