【问题标题】:Getting error on hitting Google Vision Api在点击 Google Vision Api 时出错
【发布时间】:2019-09-24 09:52:09
【问题描述】:
  const options = {
  hostname: 'https://vision.googleapis.com/v1/images:annotate?key=<some key>',
  method: 'POST',
  headers: {
    'Content-Type' : 'application/json'
  }
};

const req = http.request(options, (res : any) => {
  res.on('data', (chunk : any) => {
    console.log(`BODY: ${chunk}`);
  });
});

req.on('error', (e) => {
  console.log(e)
  console.error(`problem with request: ${e.message}`);
});

// Write data to request body
req.write(JSON.stringify(body))
req.end()

我正在尝试使用谷歌视觉功能之一,即文本检测。但是,当我点击那个 api 时,我会收到这个错误。我仔细检查了网址和其他数据。

{ Error: getaddrinfo ENOTFOUND https://vision.googleapis.com/v1/images:annotate?key=<> https://vision.googleapis.
com/v1/images:annotate?key=<key>:80
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname:
   'https://vision.googleapis.com/v1/images:annotate?key=<key>',
  host:
   'https://vision.googleapis.com/v1/images:annotate?key=<key>',
  port: 80 }

【问题讨论】:

    标签: node.js google-vision


    【解决方案1】:

    这段代码应该可以工作,只需要进行一些更改,例如我们将使用 https 模块而不是 http 模块。

    const https = require('https');
    
    const options = {
        hostname: 'vision.googleapis.com',
        path: '/v1/images:annotate?key=' + API_KEY,
        method: 'POST',
        headers: {
            'Content-Type' : 'application/json'
        }
    };
    
    let data = "";
    const req = https.request(options, (res: any) => {
        res.on('data', (chunk: any) => {
            data += chunk;
        });
        res.on('end', (chunk) => {
            console.log(`BODY: ${data}`);
        });
    });
    
    req.on('error', (e) => {
        console.log(e)
        console.error(`problem with request: ${e.message}`);
    });
    
    // Write data to request body
    req.write(JSON.stringify(body))
    req.end()
    

    【讨论】:

    • 我应该早点使用 https。
    • 哦,也许是这样.. 但有时在调试时很难看到这些东西.. :-).. Google Vision API 真的很有用...编码愉快!
    • 感谢您的帮助和更新答案。
    • 没问题,很高兴为您提供帮助..!
    【解决方案2】:

    尝试将请求修改为:

      const options = {
      method: 'POST',
      headers: {
        'Content-Type' : 'application/json'
      }
    };
    
    const req = http.request(`https://vision.googleapis.com/v1/images:annotate?key=<some key>`, options, (res : any) => {
      res.on('data', (chunk : any) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    

    因为https://vision.googleapis.com/v1/images:annotate?key=&lt;some key&gt; 是完整的 URL,而不是有效的主机名。

    【讨论】:

    • 不允许进行 https 调用
    猜你喜欢
    • 2019-02-24
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2016-06-06
    • 2019-05-11
    • 2021-03-01
    • 2023-04-02
    相关资源
    最近更新 更多