title: How do you send a JSON response in CakePHP 3.x
body: <p>I want to produce a JSON response. I've tried the following in my Controller methods:</p>
  
  <pre><code>public function removeFilter($id = null)
  {
      $this-&gt;autoRender = false;
  
      header('Content-Type: application/json');
      echo json_encode(['result' =&gt; 'filter_removed']);
  }
  </code></pre>
  
  <p>Then following instructions at <a href="https://stackoverflow.com/questions/43184833/cakephp3-4-how-to-send-a-json-object-response">CakePHP3.4: How to send a json object response?</a> I also tried:</p>
  
  <pre><code>public function removeFilter($id = null)
  {
      $this-&gt;autoRender = false;
  
      return $this-&gt;response
      -&gt;withType('application/json')
      -&gt;withStringBody(['result' =&gt; 'filter_removed']);
  }
  </code></pre>
  
  <p>Both of these give a Response Headers of <code>Content-Type: text/html; charset=UTF-8</code>. There is no template associated with this Controller method, which is why <code>autoRender = false</code>.</p>
  
  <p>What's going wrong here?</p>
  
  <p>CakePHP 3.5.13</p>