调用 API 时解析正文时出错

There was an error parsing the body when calling an API

我正在做一个 OCR 项目,我正在尝试使用 vidado API。当我通过 posman 发送 post 请求时,它给了我正确的响应,但是当我从 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
                    ],
                ]);

in postman 我的请求如下

有人注意到真正的错误吗?所以请给我一个方法来做到这一点。 谢谢。

根据Guzzle documentation

Note

multipart cannot be used with the form_params option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.

This option cannot be used with body, form_params, or json

因此您不能将 form_params 与 multipart/form-data 一起使用,您必须以这种方式使用多部分方法:

$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
        ]
    ]
]);