0

I´m creating an application which uses the ocr space api to retrieve text from uploaded images. I´ve tested the api using postman and tested also the generated code snippet for Node.js using the request library for the api call. As the request module is deprecated I want to use axios to perform my api calls but I´m still failing. Here is my coding so far. For handling the file upload in the post request I´m using the form-data module.

const axios = require('axios');
const formData = require('form-data');
const form = new formData();

const stream = fs.createReadStream('path-to-my-file');
form.append('file', stream);
form.append('language', 'eng');
form.append('filetype', 'png');

let url = 'https://api.ocr.space/parse/image'
let options = {
    headers: {
        'apikey': 'my-api-key'
      }
};

axios.post(url, form, options)
      .then(function (response.data) {
          console.log(response);
      }).catch(function (error) {
          console.error(error);
    });

Running this code will cause the following error:

OCRExitCode: 99,
  IsErroredOnProcessing: true,
  ErrorMessage: [
    "Parameter name '----------------------------218784067201800707615554\r\n" +
      "Content-Disposition: form-data; name' is invalid. Valid parameters: apikey,url,language,isoverlayrequired...

Any hints where I´m going wrong?

stefano
  • 315
  • 2
  • 16

1 Answers1

3

I just had the same issue. I solved by adding the headers that comes with the form:

let options = {
    headers: {
        'apikey': 'my-api-key',
         ...form.getHeaders()
      }
};

Hope you solved, and if you did, please share the answer. I was looking for it!

fcdt
  • 2,371
  • 5
  • 14
  • 26
Manuel Guzman
  • 483
  • 5
  • 15