2017-06-22 130 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