作为一个 POC,我希望能够拍摄我的收据图片(如加油、购物等),并使用聊天机器人将它们发送到我的会计软件。我的问题在于使用其 API 将收集的收据(图像)发送到会计软件。
第一步(获取附件)会生成一个包含图像的 Arraybuffer。我使用了 NodeJS 的示例之一(编号 15)来完成此操作。
这会导致400个错误请求。可能API需要一个文件,而我不能只发送缓冲区。我使用Postman进行测试,并且在本地存储图像文件的情况下,请求被接受,使用application/x-www-form-urlencoded 。如何最好地发布在缓冲区数组中检索到的图像?
第一步(获取附件)会生成一个包含图像的 Arraybuffer。我使用了 NodeJS 的示例之一(编号 15)来完成此操作。
const attachment = turnContext.activity.attachments[0];
const url = attachment.contentUrl;
let image;
axios.get(url, { responseType: 'arraybuffer' })
.then((response) => {
if (response.headers['content-type'] === 'application/json') {
response.data = JSON.parse(response.data, (key, value) => {
return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
});
}
image = response.data;
}
).catch((error) => {
console.log(error);
});
我在第二部分遇到了困难。即将图像发布到会计软件中。
const requestConfig = {
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
axios.post(postUrl, image, requestConfig)
.then((response) => { console.log(response); }
).catch((error) => {
console.log(error);
});
};
这会导致400个错误请求。可能API需要一个文件,而我不能只发送缓冲区。我使用Postman进行测试,并且在本地存储图像文件的情况下,请求被接受,使用application/x-www-form-urlencoded 。如何最好地发布在缓冲区数组中检索到的图像?