测试时如何在请求中传递“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 7,您可以访问他们的文档以获取 Testing JSON APIs .
关于php - 如何通过 Accept : application json header in call, 发布请求 Laravel 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944391/