php - 如何通过 Accept : application json header in call, 发布请求 Laravel 5.1

标签 php json laravel laravel-5.1

测试时如何在请求中传递“Accept: application/json”:

例如:

$response = $this->post($route, $data, ['Accept: application/json']);

$response = $this->call('post' $route, $data, [], [], ['Accept: application/json']);

我想在 FormRequest 类中调用 $this->wantsJson():

/**
     * Get the proper failed validation response for the request.
     *
     * @param  array  $errors
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function response(array $errors)
    {
        if ($this->ajax() || $this->wantsJson()) {
            return new JsonResponse($errors, 422);
        }

        return $this->redirector->to($this->getRedirectUrl())
                                        ->withInput($this->except($this->dontFlash))
                                        ->withErrors($errors, $this->errorBag);
    }

最佳答案

你可以使用$this->json()

Visit the given URI with a JSON request.

这是来自 Laravel 的 API 的链接:

Laravel 5.1 API

Laravel 5.2 API

编辑:对于 Laravel 7,您可以访问他们的文档以获取 Testing JSON APIs .

关于php - 如何通过 Accept : application json header in call, 发布请求 Laravel 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944391/

相关文章:

javascript - 存储超过 4 位数年份的日期

php - Mysql 与其他表的结果一起排序(COUNT ROWS OF TABLE 2)

java - 将 JSONPath 过滤器应用于带空格的字段

javascript - 如何像这样重定向到其他网站

php - Laravel 4.2 MethodNotAllowedHttpException 删除

php - Laravel5 Eloquent 方法全部返回意外结果

php - 如何在递归中增加深度变量?它不断重置

php - 用php显示mysql数据库中的图像

javascript - ReactJS 游戏 - 在组件之间切换 - 无法执行

php - 每个帖子请求的 CSRF token 不匹配 Laravel 9