使用Nodejs向Google API Dialogflow请求时出现错误ENOTFOUND

时间:2018-05-10 22:03:08

标签: node.js https google-api dialogflow

我有一个Google Dialogflow应用程序,当我使用其他应用程序(如失眠邮递员)向api发出请求时,它可以正常工作。但是,当我使用nodejs时,它会返回一个错误: ["(...)"由我添加]

> getaddrinfo ENOTFOUND
> dialogflow.googleapis.com/v2/projects/(...):detectIntent
> dialogflow.googleapis.com/v2/projects/(...):detectIntent:80

这是我在节点上的代码:

var datajson = '{"queryInput":{"text":{"text": "hola", "languageCode": "es"}},"queryParams":{"timeZone":"America/Santiago"}}';

var options = {
  host: 'https://dialogflow.googleapis.com/v2/projects/si(...):detectIntent',
  method: 'POST',
  json: datajson,
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
    'Authorization': 'Bearer ya29.c.El-3sdsa(...):detectIntent' 
  }
}

var req = http.request(options, function(res) {
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

在失眠中:

Example of DialogFlow working on Insomnia

Example of DialogFlow working on Insomnia

请帮忙吗? 感谢

1 个答案:

答案 0 :(得分:0)

有时仅由于没有网络连接而返回ENOTFOUND错误代码,因此无法访问API服务器。

以下是在这种情况下返回的实际错误消息的示例:

class MockFirebaseClient extends Mock implements Firestore {} //for your mock injection

class MockCollectionReference extends Mock implements CollectionReference {} //for when declaration

class MockQuerySnapshot extends Mock implements QuerySnapshot {} //for the thenAnswer return on collection of docs

class MockDocumentReference extends Mock implements DocumentReference {} //for single doc query

class MockDocumentSnapshot extends Mock implements DocumentSnapshot {} // for the thenAnswer return on single doc query