Node.js 点击Google Vision Api时出错 const选项={ 主机名:'https://vision.googleapis.com/v1/images:annotate?key=', 方法:“POST”, 标题:{ “内容类型”:“应用程序/json” } }; const req=http.request(选项,(res:any)=>{ res.on('数据',(块:任何)=>{ log(`BODY:${chunk}`); }); }); 请求开启('错误',(e)=>{ 控制台日志(e) 错误(`problemwithrequest:${e.message}`); }); //将数据写入请求主体 请求写入(JSON.stringify(body)) 请求结束()
我正在尝试使用谷歌视觉功能之一,即文本检测。但当我碰到那个api的时候,我就得到了这个错误。我仔细检查了url和其他数据Node.js 点击Google Vision Api时出错 const选项={ 主机名:'https://vision.googleapis.com/v1/images:annotate?key=', 方法:“POST”, 标题:{ “内容类型”:“应用程序/json” } }; const req=http.request(选项,(res:any)=>{ res.on('数据',(块:任何)=>{ log(`BODY:${chunk}`); }); }); 请求开启('错误',(e)=>{ 控制台日志(e) 错误(`problemwithrequest:${e.message}`); }); //将数据写入请求主体 请求写入(JSON.stringify(body)) 请求结束(),node.js,google-vision,Node.js,Google Vision,我正在尝试使用谷歌视觉功能之一,即文本检测。但当我碰到那个api的时候,我就得到了这个错误。我仔细检查了url和其他数据 const options = { hostname: 'https://vision.googleapis.com/v1/images:annotate?key=<some key>', method: 'POST', headers: { 'Content-Type' : 'application/json' } }; const
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()
{错误:getaddrinfo ENOTFOUNDhttps://vision.googleapis.com/v1/images:annotate?key= https://vision.googleapis.
com/v1/images:annotate?key=:80
在GetAddrInfoReqWrap.onlookup[as oncomplete](dns.js:56:26)
errno:'ENOTFOUND',
代码:“ENOTFOUND”,
系统调用:“getaddrinfo”,
主机名:
'https://vision.googleapis.com/v1/images:annotate?key=',
主持人:
'https://vision.googleapis.com/v1/images:annotate?key=',
端口:80}
尝试将请求修改为:
{ 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 }
const选项={
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”
}
};
const req=http.request(`https://vision.googleapis.com/v1/images:annotate?key=`,选项,(res:any)=>{
res.on('数据',(块:任何)=>{
log(`BODY:${chunk}`);
});
});
因为
https://vision.googleapis.com/v1/images:annotate?key=
是一个完整的URL,不是有效的主机名。此代码应该可以工作,只需进行几项更改,例如,我们将使用https模块而不是http模块
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的。哦,也许是这样。。在调试时,有时很难看到这些东西…-)。。谷歌视觉API真的很有用。。。快乐编码!谢谢你的帮助和更新答案。没问题,很高兴能帮上忙!它不允许进行https呼叫
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()