The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

PHP built-in server overwrites "content-Type' header

David Rodger's Avatar

David Rodger

29 Jul, 2015 04:43 AM

I'm writing an API in Slim3 and serving it on my dev machine using PHP's built-in server. I'm setting headers to make CORS work, but even though I set 'Content-Type' to 'application/json', I'm finding that the server always responds with 'text/html; charset=utf-8'. I can fix that with a direct call to the header() function just before $app->run(), but that kind of defeats the purpose of Slim's response object methods.

Has anyone else come across this, and were you able to fix it?

  1. 1 Posted by David Rodger on 29 Jul, 2015 06:53 AM

    David Rodger's Avatar

    I did a bit of digging. I placed a call to headers_list() in \Slim\App::respond()-- after headers were sent but before the body was sent. This is the result:

    Array
    (
        [0] => X-Powered-By: PHP/5.6.8
        [1] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
        [2] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
        [3] => Pragma: no-cache
        [4] => Content-type: text/html;charset=UTF-8
        [5] => Content-Type: application/json
        [6] => Access-Control-Allow-Credentials: true
        [7] => Access-Control-Allow-Origin: http://localhost:9000
        [8] => Access-Control-Allow-Methods: GET, POST, PUT, DELETE
        [9] => Content-Length: 701
    )
    
    ...in both PHP's built-in server and Apache.

    (Incidentally, I hadn't thought about .htaccess, but it appears the built-in server can read it, because I can use the URL http://localhost:8888/path and not have to use http://localhost:8888/index.php/path.)

    Apache sends the last 'Content-Type' header, whereas the built-in server sends the first, which is why placing a direct call to header() before $app->run() works.

    I can modify https://github.com/slimphp/Slim/blob/3.x/Slim/App.php#L345 to:

    header(sprintf('%s: %s', $name, $value), true);
    
    but this may prevent multiple headers of the same type which might be required for some apps.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac