简体   繁体   中英

Error: getaddrinfo ENOTFOUND when sending https request Node.js and API

I am developing a dashboard, I need to conect to a API and catch a Auth Token and afther that send info by using a HTTPS protocol. I use a Nodejs, and when I run my code I have the next error on the pm2 monit:

 Error: getaddrinfo ENOTFOUND my.url.net/path at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'my.url.net/path' }

Also here is my code where I made the request (Node.js):

const path = require('path');
require('dotenv').config({path: path.join('path','.env')});
const https     = require('https');
const database = require('./sql');
const fs         = require ('fs');

const user = process.env.USER;
const pwd  = PWD;
const host = 'https://my.url.net/extencio';
const host_1 = 'my.url.net/extention';

async function getLoginToken(pForce){

 if (login_token.Health && !pForce) { return login_token }

  //Creates the POST request
  const options = {
    protocol: 'https:',
    hostname: host_1,
    path: '/api/auth/token',
    method: 'POST',
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
        }
  };

  //Body of the POST request, contains the user and password
  const post_data = JSON.stringify({username: user, password: pwd});
.

Here is the rest of the code:

 return new Promise((resolve, reject) => { const req = new https.request(options, (response) => { response.setEncoding('utf8'); response.on('data', function(chunk){ const output = JSON.parse(chunk); if(output.token){ login_token.Health = true; login_token.Token = output.token; resolve(login_token) } else{ login_token.Health = false; login_token.Token = ''; resolve(login_token); } }); }); req.write(post_data); req.end(); req.on('error', function(err) { console.log(err); login_token.Health = false; login_token.Token = ''; resolve(login_token) }); }); }

It seems that is trying to getaddrinfo of the full url, instead of just the hostname. I would put hostname in option as only "my.url.net" and update path with the rest of the url.

@Eric0607 the error stackoverflow.com/questions/65810720/… you've provided is not showing anymore, I might've been too late to reply.

but in case you got " an invalid local cert SSL error ", here is a fix I found that works for it. disable SSL check in your code , not recommended but it would work temporarily, turn it on after you're done or it can be risky.

Remove protocol, and use domain names only for the host. For instance:

Wrong:

const host = 'https://my.url.net/extencio'
const path = '/api/auth/token'

Correct:

const host = 'my.url.net'
const path = '/extencio/api/auth/token'

See documentation for the http.request options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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