I am trying to get (StatusCode) in response of REST api while its just return field name and error message like this
[{"field":"Email","message":"Email \"ali@ali.ali\" has already been taken."}]
I have added response
'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;
}
},
],
Try that way, it works for me:
if ("some error checking goes there") {
Yii::$app->response->statusCode = 422;//I preferred that error code
return [
"data" => [
'errors' => [
'fieldname' => "Field Name is invalid",
]
],
];
}
Yii::$app->response->statusCode
您可以在回复之前添加“执行此代码”操作
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.