2016-03-11 22 views
2

我如何通過 「接受:應用/ JSON的」 請求中的測試時:如何通過接受:應用JSON頭呼叫,POST請求Laravel 5.1

爲如:

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

回答

2

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

使用JSON請求訪問給定的URI。

下面是Laravel的API鏈接:

Laravel 5.1 API

Laravel 5.2 API

+0

是在5.1這個可用? –

+0

我更新了我的答案。 –