2017-04-24 96 views
0

我在写一个Alexa技巧,它可以对Google日历进行API调用,但是我的GET请求不起作用。节点GET请求不起作用

var request = require('request'); 
console.log("Before requests"); 

// getUrl is the the url and returns the data when entered into my browser    
request(getUrl, function (error, response, body) { 
    console.log('error:', error); 
    console.log('statusCode:', response && response.statusCode); 
    console.log('body:', body); 
}); 

我使用request使HTTP调用这个代码是从字面上刚刚从github page复制,但没有被记录到控制台,所以我不知道如何调试什么错。使用其他方法,如http.get(options, callback)产生了相同的结果 - 回调中的任何console.log()都不会被执行。有没有人有任何见解?谢谢!

-

一些其他的东西 - 我的console.log("Before requests");显示出来,而我的getURL可能没有正确的API密钥提出请求,但仍然应该不是返回不工作的错误。

此代码通过Amazon Lambda函数(AWS)运行,并且通过AWS Cloudwatch查看日志。看起来,如果我在我的终端node index.js中运行我的代码,请求就会很好。

-

console.log(request);输出:

jar: [Function], 
     [ 'accept', 
     'accept-charset', 
     'accept-encoding', 
     'accept-language', 
     'accept-ranges', 
     'cache-control', 
     'content-encoding', 
     'content-language', 
     'content-location', 
     'content-md5', 
     'content-range', 
     'content-type', 
     'connection', 
     'date', 
     'expect', 
     'max-forwards', 
     'pragma', 
     'referer', 
     'te', 
     'user-agent', 
     'via' ], 
    defaultProxyHeaderExclusiveList: [ 'proxy-authorization' ] }, 
    initParams: [Function: initParams], 
    debug: [Getter/Setter] 
+0

我之前没有使用'request' - 但是出于好奇,如果'console.log(request)'会发生什么? (在调用它之前,当然) – skwidbreth

+0

可能是传入的url吗? – heyitsjhu

+0

什么是网址? – chenkehxx

回答

2

想通了 - 让我的API调用,我直接移动到this.emit(:tell, ".....");允许API响应返回之前有效完成程序后。我必须将API调用包含在承诺中,以确保它在告诉Alexa响应之前运行。

0

这是否工作正常?

var options = { 
    url: $link, //paste your link here 
    method: 'GET'//or POST or whatever you want 
}; 
request(options, function(error, response, body) { 
    console.log(body); 
}); 
+0

不幸的不是。我认为将代码放入AWS lambda并从Amazon Developer控制台运行它是个问题,但我不确定这会是一个问题。 – mcheah