小编Bro*_*Ban的帖子

Node.js发送外部API POST请求

我试图将POST请求从我的angularjs控制器发送到nodejs服务器,然后应将完整的POST请求发送到外部API,这样就避免了CORS请求并使其更加安全,因为我正在发送相对私有的数据此POST请求。

我的用来向nodejs服务器发出发布请求的angularjs控制器函数看起来像这样,并且工作正常:

var noteData = {
    "id":accountNumber,
    "notes":[
        {
            "lId":707414,
            "oId":1369944,
            "nId":4154191,
            "price":23.84
        }
    ]
}

var req = {
    method: 'POST',
    url: '/note',
    data: noteData
}

$http(req).then(function(data){
    console.log(data);
});
Run Code Online (Sandbox Code Playgroud)

现在问题出在我的nodejs服务器上,我似乎无法弄清楚如何正确发送带有自定义标头的POST请求并传递JSON数据变量。

我已经使用了nodejs https函数,因为我需要访问的URL是一个https而不是http,所以我也尝试了request函数,但是没有运气。

我知道我发送的url和数据是正确的,因为当我将它们插入Postman时,它会返回我希望它返回的内容。这是我对nodejs服务器的不同尝试:

使用body-parser可以正确解析和检索来自angularjs请求的数据

尝试使用请求:

app.post('/buyNote', function (req, res) {
var options = {
    url: 'https://api.lendingclub.com/api/investor/v1/accounts/' + accountNumber + '/trades/buy/',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': apiKey
    },
    data = JSON.stringify(req.body);
};


 request(options, function (error, response, body) {
     if (!error) {
         // Print out the response body
         // …
Run Code Online (Sandbox Code Playgroud)

https post node.js express angularjs

2
推荐指数
1
解决办法
4480
查看次数

标签 统计

angularjs ×1

express ×1

https ×1

node.js ×1

post ×1