我是nodeJS的初學者。調用一個POST API的fron節點JS
我所試圖做的事:
我有一個POST API主持和當我打電話使用郵遞員它,它給了我正確的響應,如下圖所示的圖像:
但當我嘗試使用節點的NodeJS休息客戶端(https://www.npmjs.com/package/node-rest-client)打它它給我一個不相干的大對象如下:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: true,
endEmitted: true,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [ [Function: responseOnEnd], [Function] ],
data: [Function],
error: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
socket:
Socket {
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
我需要幫助才能獲得適當的對象,而我無法弄清楚我在這裏做錯了什麼。
下面是我的代碼調用REST API:
app.get('/notes', cors(), function(req, res) {
var args = {
"hiveMachineIp": "192.168.0.110",
"hiveMachinePort": "10000",
"hiveUsername": "nt",
"hivePassword": "",
"hiveDatabaseName": "default",
"hiveTableName": "transactions_24m",
"hiveAggregationColumn": "customerid",
"hiveAggregationFunction": "HISTOGRAM",
"hiveAggregationHistogramBin": "5"
};
var Client = require('node-rest-client').Client;
var client = new Client();
client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, function (data, response) {
// parsed response body as js object
// console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("postMethod", "http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", "POST");
client.methods.postMethod(args, function (data, response) {
// parsed response body as js object
// console.log(data);
// raw response
console.log(response);
});
})
我的猜測是你在某種程度上傾銷客戶端,而不是客戶端收到的服務器響應。如果您不介意共享負責創建休息客戶端和請求的部分客戶端代碼。 –
使用axios解決了問題,但我仍然想知道上面的代碼中有什麼問題。 –