php - Laravel/Guzzle - POST 二进制文件内容
问题描述
我正在尝试向第 3 方 API 发出简单的 POST 请求。API 接受文件作为 BODY 参数。具体来说,文件内容必须是二进制的:
我已经使用 Postman 成功发布到这个第 3 方 API,正文配置如下:
上传文件时,我使用的是 Laravel。这是代码:
//$document is a class of "UploadedFile"
$tempfile = Storage::disk('local')->putFile('/temp', $document);
然后我只是尝试使用 Guzzle 发布此文件:
use Illuminate\Support\Facades\Http;
$filepath = storage_path('app/' . $tempfile);
$post = Http::attach('file', file_get_contents($tempfile), 'myfile.pdf')->post('example.org')->json());
但是,第 3 方 API 无法将其识别为 PDF 文件:
内容类型必须是 application/vnd.hypatos.ocr+json application/pdf image/tiff image/jpeg image/png text/xml application/xml 之一
与我在 Postman 中发送的请求相比,我做错了什么?这是我试图发布到 API 的完全相同的文件。
解决方案
您是否尝试过Content-Type
像这样在请求标头中指定您的
Http::attach('file', file_get_contents($tempfile), 'myfile.pdf')->withHeaders([
'Content-Type' => 'application/pdf',
])->post('example.org')->json();
推荐阅读
- python - 如何在 Flask after_request 中访问请求上下文?
- c - 基于 nginx 前缀的位置处理实现的效率如何?
- datepicker - NGX-Bootstrap Datepicker当我在我的ipad上打开它时它没有在第一次点击时选择日期
- bash - 用 awk 迭代一些 thousend 文件并在一两次运行中写入相同的文件
- java - 编译 gremlin 遍历查询的响应
- c++ - 假想CPU解码指令
- python - 如何通过 OCR 将扫描的 PDF 转换为 Excel?
- json - Elasticsearch.NET (NEST) 在版本 7.x 中无法反序列化我的 POCO JSON
- laravel - 适用于 Windows 10 上 PHP 7.3 的 Apache Cassandra 驱动程序,带有 Laravel 5.8
- selenium - isDisplayed() 不工作,正在使用 pagefactory