Laravel HTTP客户端Post响应保存文件



发送post请求到Laravel控制器中的另一个api。它返回.pdf文件作为响应。我想把这个文件存起来。我得到FileNotFound异常。

代码

public function cvToPDF(Request $request)
{
$response = Http::withHeaders(['Content-Type' => 'application/pdf'])
->withToken(Request()->bearerToken())
->post('http://some-endpoint', $request->all());

Storage::disk('s3')->putFile('drive/files', $response);
return $response;
}

return $response在客户端工作。我可以用这个端点下载pdf。但是Storage::disk引发异常

因为您试图保存响应,而不是文件。

sink()方法的解决方法-这可能有效。

public function cvToPDF(Request $request)
{
$tempName = tempnam(sys_get_temp_dir(), 'response').'.pdf';
$response = Http::sink($tempName)
->withHeaders(['Content-Type' => 'application/pdf'])
->withToken(Request()->bearerToken())
->post('http://some-endpoint', $request->all());
Storage::disk('s3')->putFile('drive/files', new File($tempName));
return $response;
}

最新更新