2017-02-17 51 views
0

我正尝试使用请求模块调用外部API,当调用本地托管的API但在调用外部API时没有成功时,它正常工作。注:我身后代理这可能是问题使用请求模块在代理后面调用外部REST api

代码

app.get('/Get', function(req, res){ 

request.get('https://api.github.com/repos/request/request', 

function(error, response, body) { 

if (!error && response.statusCode === 200) { 
     res.send(response.body);} 
else { 
     res.json(error); 
    } 
}); 
}); 
app.listen(8080); 
console.log("server is running at 8080"); 

它是否工作正常,如果我打电话给当地的API波纹管:

request.get('http://localhost:8080/hello', 

回答

0

代理是问题,PFB解决方案如何设置代理并添加x-api-key标头

request({uri:'https://api.github.com/repos/request/request', proxy:'http://proxy.server.com', 
headers:{ 
'x-api-key':'asdfasdfsda'} 
},