如何在调用中传递Accept:application json标头,发布请求Laravel 5.1

时间:2016-03-11 15:44:23

标签: php json laravel laravel-5.1

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

例如:

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

OR

$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);
    }

1 个答案:

答案 0 :(得分:2)

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

  

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

以下是Laravel API的链接:

Laravel 5.1 API

Laravel 5.2 API