0
我正在用Node.js代碼調用API,但代碼始終引發錯誤。使用node.js調用api時出錯
基本上我創建微軟博特框架殭屍和投擲的圖像API和evalutaing響應
var restify = require('restify');
var builder = require('botbuilder');
var http = require("http");
var request= require("request");
var rest = require('rest');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() { });
// Create the chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: 'APP_ID',
appPassword: 'APP_PASS'
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
server.get('/', restify.plugins.serveStatic({
directory: __dirname,
default: '/index.html'
}));
var bot = new builder.UniversalBot(connector, function (session, results) {
var attachment = session.message.attachments[0];
request({url: attachment.contentUrl, encoding: null}, function (error, response, body) {
// Take the image and post to the custom vision service
rest.post('URL_OF_API' , {
multipart: true,
headers: {
'Prediction-Key': 'KEY',
'Content-Type': 'multipart/form-data'
},
data: {
'filename': rest.data('TEST.png', 'image/png', body)
}
}).on('complete', function(data) {
let men = 0.0; //not-men
let women = 0.0; //not-women
let topHit = { Probability: 0.0, Tag: '' };
for (var i = 0; i < data.Predictions.length; i++) {
if (data.Predictions[i].Tag === 'Men')
men = data.Predictions[i].Probability;
else if (data.Predictions[i].Tag === 'Women')
women = data.Predictions[i].Probability;
else {
if (data.Predictions[i].Probability > topHit.Probability)
topHit = data.Predictions[i];
}
}
let gender = 'No idea';
if (men > women)
gender = 'Men';
if (women > men)
gender = 'Women';
var opt='The Pic sent appears to be a'+gender+'or'+gender+'s cloth';
session.endDialog(opt);
}).on('error', function(err, response) {
session.send('Error calling custom vision endpoint');
}).on('fail', function(data, response) {
session.send('Failure calling custom vision endpoint');
});
});
});
我收到此錯誤
'filename': rest.data('TEST.png', 'image/png', body)
^
TypeError: rest.data is not a function
at Request._callback (C:\Users\Tushar\botforarticle\app.js:39:26)
at Request.self.callback (C:\Users\Tushar\botforarticle\node_modules\request\request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\Users\Tushar\botforarticle\node_modules\request\request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (C:\Users\Tushar\botforarticle\node_modules\request\request.js:1091:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)