2017-04-24 86 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