cancel
Showing results for 
Search instead for 
Did you mean: 

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

MioYasutake
Active Contributor

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.

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.

MioYasutake
Active Contributor
0 Kudos

If I change the the method from 'POST' to 'GET' and remove the body, the call is successful. So I can tell that the URL and token are properly set.

View Entire Topic
MioYasutake
Active Contributor

Passing the following object to form data, instead of blob object solved the issue.

const {url, token, fileObj} = inputs

const formData = new FormData()
const fileData = {
  uri: fileObj.path,
  size: fileObj.size,
  type: "image/jpeg",
  name: fileObj.name
}
formData.append('file', fileData)

...
chaudras
Explorer
0 Kudos
I tried this, still i am GETTING ERROR Network request failed. Some how my Build app is not able to communicate Doc Info extraction.