php - 调用 API 时解析正文出错
问题描述
我正在开发一个 OCR 项目,并且正在使用 vidado API 进行尝试。当我通过 posman 发送发布请求时,它给了我正确的响应,但是当我从 php 调用 API 时,它给了我以下错误
Client error: `POST https://api.vidado.ai/read/text` resulted in a `400 Bad Request` response: {"detail":"There was an error parsing the body"}
我的代码是
$client = new \GuzzleHttp\Client();
$url = "https://api.vidado.ai/read/text";
$requestAPI = $client->post( $url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'my apikey',
'Content-Type' => 'multipart/form-data'
],
'form_params' => [
'autoscale' => 'true',
'image'=> $img
],
]);
有人注意到实际错误吗?所以请给我一个方法来做到这一点。谢谢你。
解决方案
笔记
multipart 不能与 form_params 选项一起使用。您将需要使用其中一个。对 application/x-www-form-urlencoded 请求使用 form_params,对 multipart/form-data 请求使用 multipart。
此选项不能与 body、form_params 或 json 一起使用
因此,您不能将 form_params 与 multipart/form-data 一起使用,您必须以这种方式使用 multipart 方法:
$client = new \GuzzleHttp\Client();
$url = "https://api.vidado.ai/read/text";
$requestAPI = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'my apikey',
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'image',
'contents' => fopen('/path/to/file', 'r'),
'filename' => 'custom_filename.jpg'
],
[
'name' => 'autoscale',
'contents'=> true
]
]
]);
推荐阅读
- hyperledger-fabric - Hyperledger Composer - 错误 8:RESOURCE_EXHAUSTED
- apache - 如何使用重写,如果第一次重写没有产生结果(在不同的位置搜索)
- linux - 如何grep多个匹配一行?
- azure-devops - 将自托管代理和 Azure 托管代理组合在同一个池中
- php - 如何使 Doctrine 在多台服务器上的结果缓存失效?
- java - 这种时间格式的模式是什么?
- swift - Firebase iOS 从推送通知接收数据
- android - 如何处理轮换更改的 API 调用?
- android - AlarmManager.setAlarmClock 在错误的时间引发警报?
- c# - 是否有任何解决方案可以在统一的更新功能中初始化变量?