Javascript 从节点JS调用POST-API

Javascript 从节点JS调用POST-API,javascript,node.js,post,node-rest-client,Javascript,Node.js,Post,Node Rest Client,我是nodeJS的初学者 我想做的是: 我有一个POST API托管,当我使用postman调用它时,它会给我正确的响应,如下图所示: 但当我尝试使用NodeJS节点rest客户端()点击它时,它给了我一个不相关的大对象,如下所示: IncomingMessage { _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: BufferList {

我是nodeJS的初学者

我想做的是:

我有一个POST API托管,当我使用postman调用它时,它会给我正确的响应,如下图所示:

但当我尝试使用NodeJS节点rest客户端()点击它时,它给了我一个不相关的大对象,如下所示:

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.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});

在代码中,您将执行两次请求,一次是通过
client.post
,然后调用以前注册的
client.methods.postMethod
。您应该做一个或另一个,每个回调中的
response
变量都来自http客户端,而不是您的原始json响应。您的数据已经被解析,并且它位于
数据
变量中

为了向rest服务器发送一个post请求,您应该使用
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);
        }
    }
}
然后对于
客户端.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);

我的猜测是,您以某种方式转储了客户端,而不是客户端接收到的服务器的响应。如果您不介意共享负责创建rest客户端和请求的部分客户端代码。问题已使用axios解决,但我仍想知道上述代码中的错误。这对我来说确实有效。但是,如果有人能够解释的话,那么早些时候它不起作用的原因是什么?@AnKarthik,因为你试图打印原始响应数据。它包含与您的请求和响应相关的所有信息和数据。@AnKarthik如果您只想获取响应正文数据,请取消注释
console.log(数据)
,并删除代码中的
console.log(响应)