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 \"[email protected]\" 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",
]
],
];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With