2017-06-22 132 views
0

所以可以說我有一個URL的NodeJS POST請求沒有工作

https://someAPI.com/authorize?x=val1&y=val2 

其中X和Y是我在 送我怎樣才能做到以這種方式後請求的負載值?

我已經試過這樣:

var https = require('https') 

var options = { 
    url: 'https://someAPI.com/authorize?x=val1&y=val2', 
    method: 'POST', 
    headers: { 
     .... 
    } 
} 

https.request(options, function(res) { 
    var str = "" 
    res.on('data', function(data){ 
     str+=data 
    }) 
    res.on('end', function() { 
     console.log(str) 
    }) 
}) 

我沒有得到任何東西回來。我究竟做錯了什麼?

編輯:這是我相信格式:

REQUEST 
POST https://someAPI.com/us/oauth/v2/authorize?response_type=code&client_id=blahblahblah&redirect_uri=someOtherSite.com HTTP/1.1 
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml 
User-Agent: RestSharp/105.2.3.0 
Content-Type: application/x-www-form-urlencoded 
Host: someAPI.com 
Content-Length: 98 
Accept-Encoding: gzip, deflate 

username=someuser&password=somepassword&action=Log%20In&sessionID=someSessionId 
+0

是失蹤' ''在'要求(' HTTPS) '在你的問題中拼寫錯誤還是逐字從你的代碼中? –

+0

這是我的帖子中的一個錯字。我會解決它。 –

+0

「url」呢?它不應該是一個'字符串呢? – sjahan

回答

0

變化:

https.request(options, function(res) { 
    var str = ""; 
    res.on('data', function(data){ 
     str+=data; 
    }); 
    res.on('end', function() { 
     console.log(str); 
    }); 
}); 

要:

var apiCall = https.request(options, (res) => { 
    console.log('statusCode:', res.statusCode); 
    console.log('headers:', res.headers); 

    res.on('data', (d) => { 
     process.stdout.write(d); 
    }); 
}); 

apiCall.on("error", (e) => {console.error(e)}); 
apiCall.end(); 
+0

好酷!這將返回html。狀態碼爲500.我知道這並不好,但我認爲這與我的憑證有關,而不是要求。非常感謝你。 –

+0

該文檔顯示爲選項傳遞的不同值。我會看看https://nodejs.org/api/https.html#https_https_request_options_callback – Christian4423

+0

看起來它只需要一個完全編碼的URL(如連接字符串),或者將值傳遞到像{port :443}所以它可以構建URL – Christian4423