madusanka
madusanka

Reputation: 163

There was an error parsing the body when calling an API

i'm working on an OCR project and i'm trying it using vidado API. when i send a post request through the posman it gives me the correct response but when i calling API from php it gives me below error

Client error: `POST https://api.vidado.ai/read/text` resulted in a `400 Bad Request` response: {"detail":"There was an error parsing the body"}

my code is

$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 my request is like below request header request body

any one noticed the actual error? so please give me a way to do this. Thank you.

Upvotes: 2

Views: 4954

Answers (1)

krylov123
krylov123

Reputation: 772

Accoring to 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

So you can't use form_params with multipart/form-data and you have to use multipart approach this way:

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

Upvotes: 1

Related Questions