2017-10-08 31 views
1

我嘗試使用機器人仿真工具,併發送此圖像關閉的微軟customvision API附加圖像的POST請求,我遇到的問題是,我得到瞭如何使用CustomVision與API的NodeJS

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

從定製回來的定製視覺API調用。

我使用的request模塊從npm處理呼叫

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

我相信,我可以通過發出錯誤的格式定製視覺不過,我一直無法弄清楚作爲然而。

+0

您可以添加更多的細節關於你所使用的圖像(這是圖像格式,圖像大小...)。 你直接嘗試首先使用自定義視覺與圖像? –

回答

0

我複製你的問題,它看起來像這個問題是你的「內容類型」。您正在嘗試通過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暴露你的本地主機。