2

I'm working on a REST API using Yii2 and I'm trying to customize the error response. By default, if I use the wrong credentials when submitting a request I see this:

{
    "name": "Unauthorized",
    "message": "You are requesting with an invalid credential.",
    "code": 0,
    "status": 401
}

Where and how can I remove the code and name lines?

2 Answers 2

2

try this code in your application configuration.

return [
    // ...
    'components' => [
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],
    ],
];

For more Details

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Just what I was looking for.
0

I have new solution:

return [
    // ...
    'components' => [
        'response' => [
            'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;

                // exception with http code != 200 and 302
                if ($response->statusCode !== 200 && $response->statusCode !== 302) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'message' => YII_DEBUG ?$response->data: 'System error'
                    ];
                    $response->statusCode = 200;
                }
                // normal response
                else{
                    // both case: controller implement Controller or ActiveController
                    $result =  [
                        'success' => isset($response->data['success'])?$response->data['success']: true
                    ];

                   // check message is set
                if (isset($response->data['message'])){
                    $result = array_merge($result, ['message' => $response->data['message']]);
                }
                else{
                    // check data is set
                    // both case: controller implement Controller or ActiveController
                    if (isset($response->data['data']))
                        $result = array_merge($result, ['data' => $response->data['data']]);
                    else{
                        $result = array_merge($result, ['data' => $response->data]);
                    }
                }

                    $response->data = $result;
                    $response->statusCode = 200;
                }
            },
        ]
    ],
];

1 Comment

How does this improve on the previous answer? Include some explanation to your answer to make it more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.