使laravel报告格式不正确的JSON
有没有办法让laravel在输入时报告格式错误的JSON?现在控制器没有收到任何数据,如果内容类型为json on on请求,我希望它引发一个异常使laravel报告格式不正确的JSON,json,exception,laravel,Json,Exception,Laravel,有没有办法让laravel在输入时报告格式错误的JSON?现在控制器没有收到任何数据,如果内容类型为json on on请求,我希望它引发一个异常 App::before(function ($request) { if ( ! str_contains($request->getContentType(), 'json')) return; json_decode($request->getContent()); if (json_last_error(
App::before(function ($request)
{
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($request->getContent());
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
如果不希望在没有内容时失败,请使用以下方法:
App::before(function ($request)
{
if ( ! ($content = $request->getContent())) return;
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($content);
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
在before过滤器中执行此操作:
App::before(function ($request)
{
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($request->getContent());
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
如果不希望在没有内容时失败,请使用以下方法:
App::before(function ($request)
{
if ( ! ($content = $request->getContent())) return;
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($content);
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
在before过滤器中执行此操作:
App::before(function ($request)
{
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($request->getContent());
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
如果不希望在没有内容时失败,请使用以下方法:
App::before(function ($request)
{
if ( ! ($content = $request->getContent())) return;
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($content);
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
在before过滤器中执行此操作:
App::before(function ($request)
{
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($request->getContent());
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
如果不希望在没有内容时失败,请使用以下方法:
App::before(function ($request)
{
if ( ! ($content = $request->getContent())) return;
if ( ! str_contains($request->getContentType(), 'json')) return;
json_decode($content);
if (json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Malformed JSON.');
}
});
getContentType只是“json”而不是“application/json”,这正常吗?无论如何,我没有通过检查,我添加了额外的“json”检查,它起了作用,所以我接受你的回答这是正常的,getContentType只是“json”而不是“application/json”?无论如何,我没有通过检查,我添加了额外的“json”检查,它起了作用,所以我接受你的回答这是正常的,getContentType只是“json”而不是“application/json”?无论如何,我没有通过检查,我添加了额外的“json”检查,它起了作用,所以我接受你的回答这是正常的,getContentType只是“json”而不是“application/json”?无论如何,我没有通过检查,我为“json”添加了额外的检查,它起了作用,所以我接受你的答案