Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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报告格式不正确的JSON_Json_Exception_Laravel - Fatal编程技术网

使laravel报告格式不正确的JSON

使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(

有没有办法让laravel在输入时报告格式错误的JSON?现在控制器没有收到任何数据,如果内容类型为json on on请求,我希望它引发一个异常

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”添加了额外的检查,它起了作用,所以我接受你的答案