Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 7:使失败的验证响应为200而不是422_Laravel_Api_Response_Laravel 7 - Fatal编程技术网

Laravel 7:使失败的验证响应为200而不是422

Laravel 7:使失败的验证响应为200而不是422,laravel,api,response,laravel-7,Laravel,Api,Response,Laravel 7,我正在创建一个Laravel7应用程序 我创建了一个POST API,它接受2个参数,并对它们进行了验证。当验证程序失败时,它给出响应“422 unprocessable entity”,并以JSON格式给出如下错误: { "message": "The given data was invalid.", "errors": { "mobile_no": [ "The mobile no must be 10 digits."

我正在创建一个Laravel7应用程序

我创建了一个POST API,它接受2个参数,并对它们进行了验证。当验证程序失败时,它给出响应“422 unprocessable entity”,并以JSON格式给出如下错误:

{
    "message": "The given data was invalid.",
    "errors": {
        "mobile_no": [
            "The mobile no must be 10 digits."
        ]
    }
}
现在我想要相同的响应,错误代码是200,而不是422


我应该如何实现这一点?

您可以为此使用错误处理程序。


检查所需的内部函数异常,并将其替换为成功响应

对于laravel 8,我将覆盖app/Exceptions/Handler.php中的invalidJson方法

protected function invalidJson($request, ValidationException $exception)
{
    return response()->json([
        'message' => $exception->getMessage(),
        'errors' => $exception->errors(),
    ], 200); //parent method return 422
}

请提供API控制器的代码
protected function invalidJson($request, ValidationException $exception)
{
    return response()->json([
        'message' => $exception->getMessage(),
        'errors' => $exception->errors(),
    ], 200); //parent method return 422
}