skip to Main Content

I’m trying to send a POST request using cURL in PHP to eden ai to use their object detection API, and I’m getting a response saying:

"The submitted data was not a file. Check the encoding type on the form."

I’m probably handling the file in a wrong way.

I also tried setting the full path of the image.

That’s the function I’m using:

public function index() {

    $ch = curl_init();

    $url = "https://api.edenai.run/v2/image/object_detection";
    $apiKey = "my_api_key";
    $imagePath = "/ise/myimage.jpg";
    
    $data = array(
        'providers' => 'api4ai, clarifai, amazon, google, microsoft, sentisight',
        'file' => $imagePath
    );
    
    $jsonData = json_encode($data);
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $apiKey
    ));
    
    $response = curl_exec($ch);
    
    if (curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    }
    
    curl_close($ch);
    
    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput($response);

}

2

Answers


  1. You can’t send file data as part of a JSON POST, that requires a multipart/form-data request.

    According to the EdenAI documentation:

    In order to use a JSON POST body for their API, you need to provide file_url instead, which is a publicly accessible URL for the image.

    This is because when you use application/json as your HTTP POST content body, there isn’t a standardized way to include the binary file data.

    Alternatively, if exposing the image at a publicly accessible URL isn’t tenable – don’t use a JSON content body and instead use a regular CURL_POSTFIELDS form body. [Edit: And this is the case where you’d use file as the field to post the file data. Don’t JSON-encode your POST fields and don’t include the content-type header in your CURLOPT_HTTPHEADER option.] Relevant PHP documentation for uploading files this way and some examples can be found here: https://www.php.net/manual/en/class.curlfile (This is also a common question on stack overflow)

    Login or Signup to reply.
  2. You must to change your code to this :

    public function index() {
        $ch = curl_init();
        $url = "https://api.edenai.run/v2/image/object_detection";
        $apiKey = "my_api_key";
        $imagePath = "/ise/myimage.jpg";
        
        $data = array(
            'providers' => 'api4ai, clarifai, amazon, google, microsoft, sentisight',
            'file' => new CURLFile($imagePath, mime_content_type($imagePath), basename($imagePath))
        );
        
        $jsonData = json_encode($data);
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization: Bearer ' . $apiKey
        ));
        
        $response = curl_exec($ch);
        
        if (curl_errno($ch)) {
            echo 'Error: ' . curl_error($ch);
        }
        
        curl_close($ch);
        
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput($response);
    
    }```new CURLFile($imagePath, mime_content_type($imagePath), basename($imagePath))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search