简体   繁体   English

对https API的IBM Web操作调用不起作用

[英]IBM web action call to https API not working

I am creating an assistant with IBM Watson. 我正在与IBM Watson创建助理。 The assistant is passing some info to a web action that calls an https API and fetches data back to the assistant in a JSON format. 助手正在将一些信息传递到Web操作,该Web操作调用https API并以JSON格式将数据取回助手。

It looks like the web action is only working and now and then a message "error: The action did not produce a valid response and exited unexpectedly." 似乎Web操作仅在工作,并且有时会显示一条消息“错误:该操作未产生有效的响应并意外退出”。 pops up. 弹出。 It is not clear when or what triggers the fault. 目前尚不清楚何时或什么原因触发故障。 Invoking the exactly same action several times within seconds leads to the error message appearing randomly (I did not change anything in the action or code between calls). 在几秒钟内多次调用完全相同的操作会导致错误消息随机出现(我在两次调用之间未更改操作或代码中的任何内容)。 When there is no error, the code is working perfecly and providing the expected answer. 如果没有错误,则代码可以正常工作并提供预期的答案。

Here the compact code: 下面是紧凑的代码:

function main(msg){
  const https = require('https');
  var reqUrl = https URL;

return new Promise(function(resolve, reject) {
    https.get(reqUrl, (responseFromAPI) => {
        responseFromAPI.on('data', (chunk) => {
               completeResponse += chunk;
               let movie_info = JSON.parse(completeResponse);
               movie_info = movie_info.results[0];
               console.log(movie_info);
               resolve({movie_info});
        })
        responseFromAPI.on('error', (error) => {
            console.log(error);
            reject(error);
        });
    });
});
}

The error log follows: 错误日志如下:

[ "2019-06-06T14:35:32.697875Z stderr: undefined:1", "2019-06-06T14:35:32.697909Z stderr: {\\"page\\":1,\\"total_results\\":76,\\"total_pages\\":4,\\"results\\":[{\\"vote_count\\":39,\\"id\\":541560,\\"video\\":false,\\"vote_average\\":5,\\"title\\":\\"The Wind\\",\\"popularity\\":37.299,\\"poster_path\\":\\"\\/kcfPHZHSQODLCWdkUVLYATNyEVo.jpg\\",\\"original_language\\":\\"en\\",\\"original_title\\":\\"The Wind\\",\\"genre_ids\\":[27,37,53],\\"backdrop_path\\":\\"\\/bqi6QBbXmkBar98HJJKEV1HFx71.jpg\\",\\"adult\\":false,\\"overview\\":\\"A supernatural thriller set in the Western frontier of the late 1800s, The Wind stars Caitlin Gerard as a plains-woman driven mad by the harshness and isolation of the untamed land.\\",\\"release_date\\":\\"2019-06-06\\"},{\\"vote_count\\":2832,\\"id\\":353491,\\"video\\":false,\\"vote_average\\":5.6,\\"title\\":\\"The Dark Tower\\",\\"popularity\\":18.474,\\"poster_path\\":\\"\\/i9GUSgddIqrroubiLsvvMRYyRy0.jpg\\",\\"original_language\\":\\"en\\",\\"original_title\\":\\"The Dark Tower\\",\\"genre_ids\\":[28,14,878,37,27],\\"backdrop_pa [“ 2019-06-06T14:35:32.697875Z stderr:undefined:1”,“ 2019-06-06T14:35:32.697909Z stderr:{\\” page \\“:1,\\” total_results \\“:76,\\ “TOTAL_PAGES \\”:4,\\ “结果\\”:[{\\ “vote_count \\”:39,\\ “ID \\”:541560,\\ “视频\\”:假,\\ “vote_average \\”:5,\\“称号\\“:\\”风\\“,\\”人气\\“:37.299,\\”海报路径\\“:\\” \\ / kcfPHZHSQODLCWdkUVLYATNyEVo.jpg \\“,\\”原始语言\\“:\\” en \\“,\\”原始标题\\“ “:\\”风\\“,\\”流派_ids \\“:[27,37,53],\\” backdrop_path \\“:\\” \\ / bqi6QBbXmkBar98HJJKEV1HFx71.jpg \\“,\\” adult \\“:false,\\”概述《风》是一部超自然的惊悚片,设定于1800年代后期的西部边境,饰演凯特琳·杰拉德(Caitlin Gerard),是一位平原妇女,被荒芜的土地和荒芜的土地所吸引。 “2019年6月6日\\”},{\\ “vote_count \\”:2832,\\ “ID \\”:353491,\\ “视频\\”:虚假,\\ “vote_average \\”:5.6,\\ “称号\\”:\\ “ The Dark Tower \\”,\\“人气\\”:18.474,\\“ poster_path \\”:\\“ \\ / i9GUSgddIqrroubiLsvvMRYyRy0.jpg \\”,\\“ original_language \\”:\\“ en \\”,\\“ original_title \\”:\\ “ The Dark Tower \\”,\\“ genre_ids \\”:[28,14,878,37,27],\\“ backdrop_pa th\\":\\"\\/pVVobDO8cezhVPvwD6EBUN0g3mt.jpg\\",\\"adult\\":false,\\"overview\\":\\"The last Gunslinger, Roland Deschain, has been locked in an eternal battle with Walter O'Dim, also known as the", "2019-06-06T14:35:32.697914Z stderr: ", "2019-06-06T14:35:32.697951Z stderr: SyntaxError: Unexpected end of JSON input", "2019-06-06T14:35:32.697955Z stderr: at JSON.parse ()", "2019-06-06T14:35:32.697959Z stderr: at IncomingMessage.responseFromAPI.on (eval at NodeActionRunner.init (/nodejsAction/runner.js:79:45), :10:38)", "2019-06-06T14:35:32.697963Z stderr: at IncomingMessage.emit (events.js:189:13)", "2019-06-06T14:35:32.697968Z stderr: at IncomingMessage.Readable.read (_stream_readable.js:487:10)", "2019-06-06T14:35:32.697972Z stderr: at flow (_stream_readable.js:931:34)", "2019-06-06T14:35:32.697976Z stderr: at resume_ (_stream_readable.js:912:3)", "2019-06-06T14:35:32.697980Z stderr: at process._tickCallback (internal/process/next_tick.js:63:19)", "unknown unknown: There was an issue while coll th \\“:\\” \\ / pVVobDO8cezhVPvwD6EBUN0g3mt.jpg \\“,” adult \\“:false,\\” overview \\“:\\”最后一个Gunslinger,Roland Deschain,与Walter O'Dim陷入了永恒的战斗,也称为“”,“ 2019-06-06T14:35:32.697914Z stderr:”,“ 2019-06-06T14:35:32.697951Z stderr:SyntaxError:JSON输入意外终止”,“ 2019-06-06T14: 35:32.697955Z stderr:at JSON.parse()“,” 2019-06-06T14:35:32.697959Z stderr:at IncomingMessage.responseFromAPI.on(在NodeActionRunner.init处评估(/nodejsAction/runner.js:79:45 ),:10:3​​8)“,” 2019-06-06T14:35:32.697963Z stderr:at IncomingMessage.emit(events.js:189:13)“,” 2019-06-06T14:35:32.697968Z stderr:在IncomingMessage.Readable.read(_stream_read.js:487:10)“,” 2019-06-06T14:35:32.697972Z stderr:在运行中(_stream_read.js:931:34)“,” 2019-06-06T14: 35:32.697976Z stderr:在resume_(_stream_read.js:912:3)“,” 2019-06-06T14:35:32.697980Z stderr:在process._tickCallback(内部/process/next_tick.js:63:19)“ ,“ unknown unknown:coll发生问题 ecting your logs. 提取您的日志。 Data might be missing." ] 数据可能丢失。“]

movie_info is a list of movies and data, hence the code only picks up the data in the position [0] (here the movie "The wind"). movie_info是电影和数据的列表,因此代码仅在[0]位置(这里是电影“风”)中拾取数据。

I contacted the API hosts and they said the API is working perfectly, hence the problem may come from the web action itself. 我联系了API主机,他们说API正常运行,因此问题可能出在网络操作本身上。

Any help is appreciated. 任何帮助表示赞赏。

You parse not all response but only first chunk 您不是解析所有响应,而是仅解析第一个块

function main(msg){
  const https = require('https');
  var reqUrl = https URL;

return new Promise(function(resolve, reject) {
    https.get(reqUrl, (responseFromAPI) => {
       const chunks = [];
        responseFromAPI
          .on('data', chunk => chunks.push(chunk))
          .on('end', _=> {
               let movie_info = JSON.parse(Buffer.concat(chunks));
               movie_info = movie_info.results[0];
               console.log(movie_info);
               resolve({movie_info});
          })
          .on('error', (error) => {
            console.log(error);
            reject(error);
        });
    });
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM