我正在使用NodeJS向我有權訪問的私有API發送PUT請求。當使用curl時,API發回JSON,並且一切都很好,但是當使用NodeJS時,它不會返回任何東西。我對此很新,而且我知道我的代碼不是最好的,但它可以工作(除了這個問題)。我想知道如何顯示JSON,我應該從終端的PUT請求中恢復。檢索PUT請求的響應
服務器的NodeJS代碼:
app.post('/myaction', function(req, res) {
var data = req.body.service;
var data1 = req.body.serviceId;
console.log(data);
console.log(data1);
res.sendFile(__dirname + '/services.html');
myURL = 'http://privateAPI.com/service_instance/' + data1;
console.log(myURL)
request({
method: 'PUT',
uri: myURL,
multipart: [{
'content-type':'application/json',
body: data
}]
}, function(error, request, body){
})
});
當我console.log(body)
我得到API的HTML。 當我console.log(request)
我得到我發送到API的JSON。 當我console.log(error)
我得到空。
鏈接我讀過,但並沒有幫助: How to retrieve POST query parameters?
How to retrieve POST query parameters?
到'request'您提供的回調接收'error'和'body',這兩者可能是決定到底是怎麼回事 –
我已經'有幫助console.logg'ed所有他們我會編輯問題併發布他們的回覆 – CtrlAltDelete