?
avatar andriesbron
andriesbron
22 Apr 2020

Steps to reproduce the issue

In following the documentation at https://docs.joomla.org/JSON_Responses_with_JResponseJson

Created a keyed array of data $api_result and echo that as a json response:

        try {
            echo new JResponseJson($api_result);
        }
        catch(Exception $e) {
            echo new JResponseJson($e);
        }
        exit;

Expected result

json string having in the response header the content-type set to "application/json".

Actual result

json string with in the response header the content-type set to "text/html"

System information (as much as possible)

Forcing the content type using:

JFactory::getDocument()->setMimeEncoding( 'application/json' );

Does not work. It requires to set the content type by PHP. So:

This DOES NOT work:

try {
    JFactory::getDocument()->setMimeEncoding( 'application/json' );
    echo new JResponseJson($api_result);
}
catch(Exception $e) {
    echo new JResponseJson($e);
}
exit;

And this DOES work:

try {
    header('Content-Type: application/json');
    echo new JResponseJson($api_result);
}
catch(Exception $e) {
    echo new JResponseJson($e);
}
exit;

Additional comments

So, the json string is build and displayed, however, the content-type not set to application/json.

avatar andriesbron andriesbron - open - 22 Apr 2020
avatar joomla-cms-bot joomla-cms-bot - labeled - 22 Apr 2020
avatar Fedik
Fedik - comment - 22 Apr 2020

the request should be with &format=json

avatar Fedik
Fedik - comment - 22 Apr 2020

and

try {
    JFactory::getDocument()->setMimeEncoding( 'application/json' );
    echo new JResponseJson($api_result);
}
catch(Exception $e) {
    echo new JResponseJson($e);
}
exit;

why this should work if you use exit() that Interrupt app execution?

avatar andriesbron
andriesbron - comment - 22 Apr 2020

I missed the query, I assumed that the function would force the Content-Type due to its name.

The exit actually does work.

However, when I use &format=json it still does not work. Though I am used to the &format=... for raw and own types as well.
One thing is sure, I instantiate a controller and perform the execute with its task. In that controller I don't perform any parent method. Also not parent::display() as I remember that is necessary for triggering the format request. If I add parent::display(), still does not work.

I might be following a different flow than required for this, not sure. If so, it might be the way I catch the task from the request which I need for now. This is my controller instantiation in the entry file:

    require_once JPATH_COMPONENT.DS.'controllers'.DS.'postprocessor.php';
    $controller = new myapplicationControllerPostprocessor();
    $controller->execute($remote_task);
    $controller->redirect();

In this exectute I echo the jsonresponse.
Nevertheless, the issue might very likely be at my side for I overlooked the format request.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/28762.

avatar andriesbron
andriesbron - comment - 22 Apr 2020

I tried this:

    JFactory::getApplication()->input->set('format', "json");
    try {

        echo new JResponseJson($api_result);
    }
    catch(Exception $e) {
        echo new JResponseJson($e);
    }
    // And no exit

The result is that my site loads with json in the component jdoc field.
Even when I set JFactory::getApplication()->input->set('format', "json"); in the entry file, no success.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/28762.

avatar andriesbron
andriesbron - comment - 22 Apr 2020

Refactoring controller instantiation to:

    $controller = JControllerLegacy::getInstance('myapplication');
    $input = JFactory::getApplication()->input;
    $controller->execute($input->getCmd('task'));
    
    $controller->redirect(); 

And calling the cms with:

    index.php?option=com_myapplication&task=postprocessor.getjob&format=json

performing the task in the controller execute and do the jsonresponse as follows:

    try {
        echo new JResponseJson($api_result);
    }
    catch(Exception $e) {
        echo new JResponseJson($e);
    }
    // No exit, exit in comment!
    //exit;

Sets the Content-Type to text/plain.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/28762.
avatar mbabker
mbabker - comment - 22 Apr 2020

You're not sending the headers if you call exit, that's why you're missing a lot of stuff there.

As for the text/plain content type, the JsonDocument constructor only sets the content type to application/json if the request sends the appropriate Accept header.

JResponseJson is not an all knowing response object, it's an encapsulation of the response body. The response headers are still at the application/document level.

avatar Fedik
Fedik - comment - 23 Apr 2020

@andriesbron your last comment should work

avatar andriesbron
andriesbron - comment - 23 Apr 2020

@Fedik @mbabker It works indeed, you knew of course. Somehow (meaning I don't know what I did) my last comment indeed produces json rather than text/plain. What I did do is activate the Accept header to json, tried it, it worked, then deactivated it, tried it and it still worked. That puzzles me, but consider it to be my less structured approach in trying to solve it.

The json is however send as attachment rather than inline, I prefer the latter.

Thanks for your help, it gave me new insights.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/28762.
avatar Fedik
Fedik - comment - 23 Apr 2020

the issue can be closed

avatar SharkyKZ SharkyKZ - change - 23 Apr 2020
Status New Expected Behaviour
Closed_Date 0000-00-00 00:00:00 2020-04-23 12:20:30
Closed_By SharkyKZ
avatar joomla-cms-bot joomla-cms-bot - change - 23 Apr 2020
Status Expected Behaviour Closed
Closed_By SharkyKZ joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 23 Apr 2020
avatar joomla-cms-bot
joomla-cms-bot - comment - 23 Apr 2020

Set to "closed" on behalf of @SharkyKZ by The JTracker Application at issues.joomla.org/joomla-cms/28762

avatar SharkyKZ
SharkyKZ - comment - 23 Apr 2020

Closed as expected behaviour.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/28762.

Add a Comment

Login with GitHub to post a comment