javascript - 如何使用 CustomVision api 和 NodeJS 发出 POST 请求

标签 javascript node.js post botframework

我正在尝试使用机器人模拟器工具附加图像并将该图像发送到微软的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/

相关文章:

javascript - 显示警报消息并重定向页面

javascript - 隐藏/显示 div 不起作用

javascript - 实现 setTimeout native 方法

node.js - 当没有连接时,你如何杀死一个redis客户端?

javascript - jquery fadein 不工作?

javascript - 为什么 Node.js htmlparser2 在解析文档时插入额外的 Node ?

node.js - 错误 : where: "raw query" has been removed, 请使用 where ["raw query", [replacements]]

java - HTTP POST查询如何计算内容长度

javascript - HTML 表单提交给出 400 错误请求

php - 服务器删除自定义 HTTP header 字段