如何传递接受:调用中的应用程序 json 标头,后请求 Laravel 5.1


How to pass Accept: application json header in call, post request Laravel 5.1

测试

时如何传递请求中的"接受:应用程序/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() .

使用 JSON 请求访问给定的 URI。

这是来自Laravel的API的链接:

拉维尔 5.1 API

拉维尔 5.2 API

编辑:对于Laravel 7,您可以访问他们的文档来测试JSON API。