【发布时间】:2019-10-03 13:39:03
【问题描述】:
作为 POC,我想为我的收据(加油站、商店等)拍照,并使用聊天机器人将它们发送到我的会计软件。我的问题与使用其 API 将收集到的收据(图像)发送到会计软件有关。
第一部分(获取附件)生成一个带有图像的 Arraybuffer。为此,我使用了其中一个 NodeJS 示例(nr 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. bad request。可能 API 需要一个文件,而我不能只发送缓冲区。我用 Postman 进行了测试,并通过使用 application/x-www-form-urlencoded (通过使用本地存储的图像文件)接受了请求。
发布在缓冲区数组中检索到的图像的最佳做法是什么?
【问题讨论】:
-
当您说“将图像发布到会计软件”时,您的意思是将图像发送到非botframework API?公共 API 是否可用?这应该会给你一些关于接受格式的线索。
-
@mdrichardson-MSFT 他们要求标头内容类型为:multipart/mixed。正文应包含一个文件。在 cURL 示例中,它声明:-F file=@/tmp/upload.pdf。猜猜我需要找到一种将arraybuffer转换为文件并发送的方法。没弄清楚怎么做。在 Facebook 频道的情况下,将 if 临时存储在文件系统上作为中间步骤可能会很困难。
标签: node.js axios content-type