php - Laravel/Guzzle - POST 二进制文件内容

标签 php laravel guzzle

我正在尝试向第 3 方 API 发出简单的 POST 请求。 API 接受文件作为 BODY 参数。具体来说,文件内容必须是二进制的:

enter image description here

我已使用 Postman 成功发布到此第 3 方 API,主体配置如下:

enter image description here

上传文件时,我使用的是 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 文件:

Content type must be one of application/vnd.hypatos.ocr+json application/pdf image/tiff image/jpeg image/png text/xml application/xml

与我在 Postman 中发送的请求相比,我做错了什么?这与我尝试发布到 API 的文件完全相同。

最佳答案

您是否尝试过在请求 header 中指定 Content-Type,如下所示

Http::attach('file', file_get_contents($tempfile), 'myfile.pdf')->withHeaders([
    'Content-Type' => 'application/pdf',
])->post('example.org')->json();

关于php - Laravel/Guzzle - POST 二进制文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63454481/

相关文章:

javascript - 在php中使用ajax提交multipart/form-data为null

php - Laravel/Eloquent 查询出错

php - 在 Guzzle 6 中设置默认查询字符串

postgresql - DEALLOCATE pdo_stmt 需要很长时间(几天),这正常吗?

php - guzzlehttp 在 Laravel 项目上抛出错误 - strtolower() : Argument #1 ($string) must be of type string, int 给出

php - 有限制地排队 Guzzle 请求

php - 更改权限后,XAMPP mysql 停止工作

php - 如何使用单个 Laravel 代码实例配置和运行具有各自数据库的多个网站

php - 从负值中删除前导零

php - Laravel:从公共(public)静态函数访问类变量(基本的 oop 问题)