Skip to Content
1
May 08, 2022 at 11:58 AM

How to upload an image to Document Information Extraction from AppGyver?

1125 Views Last edit May 10, 2022 at 12:42 PM 5 rev

I'm using AppGyver trial account. What I'm trying to achieve with AppGyver is:

1. Take a picture of invoice

2. Upload it to Document Information Extraction

To upload an image to Document Information Extraction, I've written JavaScript code below.

I've tested the same code with Node.js and it worked fine.

flow.png

const {url, imagePath, imageName, token} = inputs

try {
  const response = await fetch(imagePath)
  const blob = await response.blob()

  const formData = new FormData()
  formData.append('file', blob, imageName)

  const options = {
    clientId: "default",
    extraction: {
      "headerFields": ["senderName"],
      "lineItemFields": ["description"]
    },
    documentType: "invoice"
  };  
  formData.append('options', JSON.stringify(options))
  console.log('before sending request')
  const response2 = await fetch(url, {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + token
    },
    body: formData
  })

  console.log('response: ', response2)
  const json = await response2.json()
  console.log('json: ', json)
  return {id: json.id}
  
} catch (error) {
  console.log(JSON.stringify(error))
  const errorObj = {
    code: "9",
    message: "something went wrong",
    rawError: error
  }
  return [1, errorObj]
}

When I run the app, the logic fails at below part and an exception is caught.

  const response2 = await fetch(url, {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + token
    },
    body: formData
  })

However, the error object is empty (as I see it in the console) and I have no clue what went wrong.

Attachments

flow.png (14.6 kB)