![](/img/trans.png)
[英]How to make a POST request to a nodejs RESTful API in reactjs
[英]How to make a POST request using CustomVision api with NodeJS
我正在嘗試使用機器人仿真器工具附加圖像,並將該圖像發送到microsofts customvision api,我遇到的問題是我得到了
{ Code: 'BadRequestImageFormat', Message: '' }
從自定義返回自定義視覺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,但將content-type設置為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公開本地主機來解決此問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.