on 05-08-2022 12:58 PM
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.
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)
...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
71 | |
9 | |
8 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.