Php 如何在调用中传递Accept:application json头,post request Laravel 5.1
测试时如何在请求中传递“Accept:application/json”: 例如:Php 如何在调用中传递Accept:application json头,post request Laravel 5.1,php,json,laravel,laravel-5.1,Php,Json,Laravel,Laravel 5.1,测试时如何在请求中传递“Accept:application/json”: 例如: $response = $this->post($route, $data, ['Accept: application/json']); 或 我想在FormRequest类中调用$this->wantsJson(): /** * Get the proper failed validation response for the request. * * @param ar
$response = $this->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的链接:
编辑:对于Laravel 7,您可以访问他们的文档。这在5.1中可用吗?@IvankaTodorova-链接不再有效。Laravel 7或即将推出的8有什么问题吗?@s3c用他们的Laravel 7文档更新了我的答案&将json发布到路线,Laravel 8也是如此。(而且,它不再是即将发布的,因为它是昨天AFAIK发布的。)@IvankaTodorova是的,它是在我发表评论到两小时后的某个时候发布的。我想说它刚刚出来,但我当时是在撒谎。谢谢你的链接。顺便说一句,Laravel5的两个链接仍然是死的,所以如果找不到有效的链接,你也可以删除它们。向上投票。
/**
* 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);
}