Search code examples
typo3typoscriptfluidtypo3-8.x

How to prevent TYPO3 to render head- and body-elements when outputting JSON?


In TYPO3 8.7 I'm trying to generate JSON using FLUID.

I've created a page with dummy content and I've updated my TypoScript configuration for all pages in a folder.

TypoScript setup

[PIDinRootline = 10]
    page = PAGE
    page {
        typeNum = 0

        config {
            disableAllHeaderCode = 1
            disablePrefixComment = 1
            xhtml_cleaning = none
            admPanel = 0
            debug = 0
            metaCharset = utf-8
            additionalHeaders = Content-Type:text/json;charset=utf-8
        }
    }
[global]

I created a dummy JSON file as well, to test the output, before creating the actual content using FLUID:

api.json

{
    "hello": "world"
}

Now, this seems to work fine. But the output still includes the html- and body-elements.

Rendered output:

<html>
    <head></head>
    <body>
        {"hello": "world"}
     </body>
</html>

The documentations says:

If you want to output JSON, RSS or similar data with Fluid, you have to write the appropriate TypoScript which passes the page rendering to Extbase and Fluid respectively. Otherwise, TYPO3 will always generate the <head>- and <body>-section.

But I don't know what to do here actually. How can I get TYPO3 to not render the wrapping HTML-elements?


Solution

  • The problem was a new syntax in TYPO3 8 and newer. In newer versions, the additionalHeaders is no longer just a TEXT but an array with numeric indices. So to set the correct header type, you have to use this one:

    additionalHeaders.10.header = Content-Type:application/json;charset=utf-8