Reputation: 95
I have a Google Dialogflow application and when i do the the request to the api with a rest app (like insomnia o postman), it work. But, when i do with nodejs, it return a error: [The "(...)" added by me]
> getaddrinfo ENOTFOUND
> dialogflow.googleapis.com/v2/projects/(...):detectIntent
> dialogflow.googleapis.com/v2/projects/(...):detectIntent:80
This is my code on node:
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);
});
In Insomnia:
Help please?. Thanks
Upvotes: 0
Views: 1689
Reputation: 893
Sometimes the ENOTFOUND error code is returned simply because there's no network connection, and so the API server cannot be reached.
Here is an example of the actual error message returned in such a case:
{
message: request to https://www.googleapis.com/oauth2/v4/token failed,
reason: getaddrinfo ENOTFOUND www.googleapis.com
type: system
errno: ENOTFOUND
code: ENOTFOUND
config: {
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
...
}
}
Upvotes: 0
Reputation: 40444
That error occurs because you're using an invalid value for host
, which should be:
A domain name or IP address of the server to issue the request to. Default: 'localhost'`
And you should be using https
instead of http
package.
var options = {
host: 'dialogflow.googleapis.com',
path: '/v2/projects/si(...):detectIntent'
}
And there is no json
option in https/http.request
, you have to use req.write
req.write(jsondata);
req.end();
My recommendation is to use request or request-promise which will handle a lot of things for you.
const request = require('request');
const options = {
url: 'https://dialogflow.googleapis.com/v2/projects/si(...):detectIntent',
method: 'POST',
body: datajson,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ya29.c.El-3sdsa(...):detectIntent'
}
}
request(options, (err, response, body) => {
console.log(err, response.statusCode, body);
});
Upvotes: 2