2017-04-16 49 views
0

我是nodeJS的初學者。調用一個POST API的fron節點JS

我所試圖做的事:

我有一個POST API主持和當我打電話使用郵遞員它,它給了我正確的響應,如下圖所示的圖像:

enter image description here

但當我嘗試使用節點的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); 
    }); 

}) 
+0

我的猜測是你在某種程度上傾銷客戶端,而不是客戶端收到的服務器響應。如果您不介意共享負責創建休息客戶端和請求的部分客戶端代碼。 –

+0

使用axios解決了問題,但我仍然想知道上面的代碼中有什麼問題。 –

回答

0

您可以使用axios這樣。

axios.post('/user', { 
    firstName: 'Fred', 
    lastName: 'Flintstone' 
}) 
.then(function (response) { 
    console.log(response); 
}) 
.catch(function (error) { 
    console.log(error); 
}); 
+0

這確實對我有用。但是,如果有人能解釋,那麼早些時候它不起作用的原因是什麼? –

+0

@AnKarthik因爲您嘗試打印原始響應數據。它包含與您的請求和響應相關的所有信息和數據。 – Gloorx

+0

@AnKarthik如果您只想獲取響應主體數據,請取消註釋'console.log(data)'並在您的代碼中移除'console.log(response)' – Gloorx

0

裏面的代碼你做你的要求了兩次,一次通過client.post然後通過調用client.methods.postMethod那是以前的註冊。你應該做一個或另一個,並且每個回調中的response變量來自http客戶端,它不是你原始的json響應。您的數據已被解析,並且位於data變量中。

爲了向您的其他服務器發送一個帖子請求,您應該使用client.post或通過client.registerMethod註冊您的方法,然後client.methods.registeredMethodName發送請求。

當您需要發送相同的POST請求多次在你的代碼定義將處理該請求響應的功能,例如:

function handleResponseA(data, response) { 
    if(response.statusCode == 200){ 
     console.log(data); 
    } else { 
     switch(response.statusCode){ 
      case 404: 
       console.log("Page not found."); 
      break; 

      case 500: 
       console.log("Internal server error."); 
      break; 

      default: 
       console.log("Response status code: " + response.statusCode); 
     } 
    } 
} 

然後爲client.post

client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, handleResponseA); 
client.post("http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", args, handleResponseA); 

並通過註冊方法完成註冊方法:

client.registerMethod("postMethod", "http://163.47.152.170:8090/MachinfinityDataPreparation/machinfinitydataprep/hiveDataAggregation/", "POST"); 

然後調用註冊的方法:

client.methods.postMethod(args, handleResponseA); 
client.methods.postMethod(args, handleResponseA);