Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Content Type is Not Being Set

I am new to Phalcon. I'm starting work on an existing app and trying to get it up and running on my local environment. The app is working, except that JSON responses are being received by the browser as text/html (see https://ibb.co/iAdcxx).

This is a similar issue as this, but those fixes have not worked. Help would be appreciated.

  • I've checked my mime.types file does indeed have an entry for application/json.
  • Using NGINX version 1.13.9
  • From debugging, I can see that the app returns string-ified JSON, eg "{"data":{"error":true,"messages":{"password":"Looks like either your email or password was wrong here. Or they didn't match up."}}}". However, the header info is saved in the response and not explicity echo'ed.
  • One thing is - the app that I am working on was written in 3.0.4, while I am on 3.3 (see below for my environment). Has anything changed in the way a response is returned to the server between these releases?

JSON Reponse Method

public function set($params)
    {
        $payload     = array(1, 2, 3);
        $status      = 200;
        $description = 'OK';
        $headers     = array();
        $contentType = 'application/json';

        $response = new \Phalcon\Http\Response();
        $response->setStatusCode($status, $description);
        $response->setContentType($contentType, 'UTF-8');
        $response->setContent(json_encode($params));

        foreach ($headers as $key => $value) {
           $response->setHeader($key, $value);
        }

        echo $response->getContent();
        exit;
    }

PHP-FPM config

location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) { return 404; }
  include fastcgi_params;
  fastcgi_index index.php;
  fastcgi_buffers 16 8k;
  fastcgi_buffer_size 32k;
  fastcgi_param HTTP_PROXY "";
  fastcgi_hide_header "X-Powered-By";
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass bookretreats_php;

  fastcgi_param  REQUEST_METHOD  $request_method;
  fastcgi_param  CONTENT_TYPE    $content_type;
  fastcgi_param  CONTENT_LENGTH  $content_length;
  fastcgi_param  REQUEST_BODY    $request_body;
}

Phalcon Version Info

Phalcon DevTools (3.2.12)

Environment:
  OS: Darwin Dans-MacBook-Pro.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
  PHP Version: 7.1.14
  PHP SAPI: cli
  PHP Bin: /usr/local/Cellar/php71/7.1.14_25/bin/php
  PHP Extension Dir: /usr/local/Cellar/php71/7.1.14_25/lib/php/extensions/no-debug-non-zts-20160303
  PHP Bin Dir: /usr/local/Cellar/php71/7.1.14_25/bin
  Loaded PHP config: /usr/local/etc/php/7.1/php.ini
Versions:
  Phalcon DevTools Version: 3.2.12
  Phalcon Version: 3.3.0
  AdminLTE Version: 2.3.6

You're only returning the contents of the response, without headers. Send it instead!

$this->response->send();
exit;

Thanks for the suggetion Lajos, but that didn't work. As additional context, the method I pasted is from a pre-existing library (ToJson) and is working on production. So I don't think that is the problem.

I've tried a couple of additional things since posting:

  • Downgraded local environment to Phalcon 3.0.4, as the site is on production
  • Manually set the default response via the default_mimetype in php.ini

Neither worked, the application/json response did not cause the page to refresh or error messages to show. Please see https://ibb.co/iTGO5H



166
Accepted
answer

Turned out this was a false alarm...I had added a var_dump in the session setup. Closing this off...