如何使用CustomVision API与NodeJS进行POST请求

我试图附加一个图像使用机器人模拟器工具,并将此图像发送到microsofts customvision api,我遇到的问题是,我得到

{ Code: 'BadRequestImageFormat', Message: '' }

自定义视觉API调用返回。

我使用npmrequest模块来处理这些调用

 // 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); }); } }); 

我相信我可能会把错误的格式发送到自定义的视觉,但是我一直无法弄清楚。

我复制了你的问题,看起来问题是你的“内容types”。 您正试图在您的请求中传递JSON,但将内容types设置为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来暴露你的本地主机。