我正在尝试使用机器人模拟器工具附加图像并将该图像发送到微软的customvision api,我遇到的问题是我得到
{ 代码:'BadRequestImageFormat',消息:'' }
从自定义返回自定义视觉 API 调用。
我正在使用 npm
中的 request
模块来处理调用
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
session.send("Hello"); //session.message.text
// If there is an attachment
if (session.message.attachments.length > 0){
console.log(session.message.attachments[0])
request.post({
url: 'xxx',
encoding: null,
json: true,
headers: {
'Content-Type': 'application/octet-stream',
'Prediction-Key': 'xxx'
},
body: session.message.attachments[0]
}, function(error, response, body){
console.log(body);
});
}
});
我相信我可能向自定义视觉发送了错误的格式,但到目前为止我还无法弄清楚。
最佳答案
我复制了您的问题,看起来问题出在您的“内容类型”上。您尝试在请求中传递 JSON,但将内容类型设置为octet-stream
。请参阅下面我修改的代码:
var bot = new builder.UniversalBot(connector, function (session) {
session.send("Hello"); //session.message.text
// If there is an attachment
if (session.message.attachments.length > 0){
console.log(session.message.attachments[0])
request.post({
url: 'https://northeurope.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures',
encoding: null,
json: true,
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'Your API Key...'
},
body: session.message.attachments[0]
},
function (err, response, body) {
if (err) return console.log(err)
console.log(body);
});
}
});
当我运行此命令时,我收到错误 InvalidImageUrl
这是预期的,因为它正在本地主机上查找内容。您可以通过使用 Ngrok 公开您的本地主机来解决这个问题.
关于javascript - 如何使用 CustomVision api 和 NodeJS 发出 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46626770/