Evgeniy Lenskiy
Evgeniy Lenskiy

Reputation: 408

How to return blank body in Yii2 REST Controller?

I try to return blank body in my rest controller, but instead it returns 'null'.

I already tried to use in the last line of script

\Yii::$app->response->setStatusCode(200);

and get the same result.

I use advanced template and custom rest logic. Controller extends yii\rest\Controller

There is my configurations in main.php file

return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
    'request' => [
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ],
    ],
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => false,
        'enableSession' => false,
    ],
    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            'GET add'  => 'api/add',
            'GET feed'  => 'api/feed',
            'GET remove'  => 'api/remove',
        ],
    ],

],
'params' => $params,

];

Here is my action

public function actionAdd()
{
    try
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $request = \Yii::$app->request;

        $id = $request->get('id');
        $user = $request->get('user');
        $secret = $request->get('secret');

        if(!$id || !$user || !$secret)
        {
            $this->setStatus(500);
            return [
                'error' => 'missing parameter'
            ];
        }

        if(!$this->checkSecret($secret, [$id, $user]))
        {
            $this->setStatus(500);
            return [
                'error' => 'access denied'
            ];
        }

        $existing = Subscribers::find()->where(['user' => $user])->count();

        if($existing)
        {
            $this->setStatus(500);
            return [
                'error' => 'user already exists in database'
            ];
        }

        $subscriber = new Subscribers();
        $subscriber->user = $user;
        //$subscriber->save();

        /*
         * expected 200 OK and blank body
         * returns 200 OK and 'null'
         */
        return \Yii::$app->response->setStatusCode(200)->send();
    }catch (\Exception $exception)
    {
        $this->setStatus(500);
        return [
            'error' => 'internal error'
        ];
    }
}

I don't have any behaviors, maybe I should?

Upvotes: 1

Views: 1687

Answers (1)

rob006
rob006

Reputation: 22174

Empty string is invalid JSON, so returning response with empty body and content-type: application/json header is incorrect. You may want to use Response::FORMAT_RAW instead of Response::FORMAT_JSON in that case:

Yii::$app->response->format = Response::FORMAT_RAW;
return;

This will return empty body with content-type: text/html header.

But if you really want to pretend that your response is JSON and return empty body, you may set $content property directly - this will skip formatting of response:

Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->content = '';
return;

Upvotes: 5

Related Questions