0
我写下面的代码调用微软QnA制造商生成答案API。从QnA制造商的API与身体参数获取响应时出错
var http=require('https');
var demo=[];
console.log("Doing the Post Operations...");
// Define an demo object with properties and values. This object will be used for POST request.
var demo=JSON.stringify({"question":"hi"});
var extServerOptionsPost={
host:'westus.api.cognitive.microsoft.com',
path:'/qnamaker/v2.0/knowledgebases/<my kb id>/generateAnswer',
port:443,
method:'POST',
headers:{
'Ocp-Apim-Subscription-Key':'my key',
'Content-Type':'application/json'
}
};
var reqPost=http.request(extServerOptionsPost,function(res){
console.log("response statusCode: ",res.statusCode);
res.on('data',function(data){
console.log('Posting Result:\n');
process.stdout.write(data);
console.log('\n\n POST Operation Completed');
});
});
reqPost.write(demo);
reqPost.end();
reqPost.on('error',function(e){
\t console.error(e);
});
然而,process.stdout.write(data);
部分打印错误代码:未指定的,与消息一起 “请稍后再试”。 我认为在这里发生的事情是它正在写体参数之前返回响应。所以我的问题是,我如何获得在我的控制台中打印的API的响应。任何帮助将不胜感激。