繁体   English   中英

无法在服务器(Node.js)上获得完整的JSON响应

[英]Not getting full json response on server (nodejs)

我有一个第三方短信API:

我正在通过该api发送消息,如果将console.log作为响应 ,则它在我的服务器中如下所示:

connection:
  Socket {
    connecting: false,
    _hadError: false,
    _handle: [Object],
    _parent: null,
    _readableState: [Object],
    readable: true,
    domain: null,
    _events: [Object],
    _eventsCount: 9,
    _maxListeners: undefined,
    _writableState: [Object],
    writable: true,
    allowHalfOpen: false,
    _bytesDispatched: 182,
    _sockname: null,
    _pendingData: null,
    _pendingEncoding: '',
    server: null,
    _server: null,
    parser: [Object],
    _httpMessage: [Circular],
    read: [Function],
    _consuming: true,
    [Symbol(asyncId)]: 1805,
    [Symbol(bytesRead)]: 0 },
 _onPendingData: [Function: noopPendingOutput],
 agent:
  Agent {
    domain: null,
    _events: [Object],
    _eventsCount: 1,
    _maxListeners: undefined,
    defaultPort: 80,
    protocol: 'http:',
    options: [Object],
    requests: {},
    sockets: [Object],
    freeSockets: {},
    keepAliveMsecs: 1000,
    keepAlive: false,
    maxSockets: Infinity,
    maxFreeSockets: 256 },
 socketPath: undefined,
 timeout: undefined,
 method: 'GET',
 _ended: false,
 res: [Circular],
 aborted: undefined,
 timeoutCb: null,
 upgradeOrConnect: false,
 parser:
  HTTPParser {
    '0': [Function: parserOnHeaders],
    '1': [Function: parserOnHeadersComplete],
    '2': [Function: parserOnBody],
    '3': [Function: parserOnMessageComplete],
    '4': null,
    _headers: [],
    _url: '',
    _consumed: false,
    socket: [Object],
    incoming: [Circular],
    outgoing: [Circular],
    maxHeaderPairs: 2000,

我的api代码(nodejs):

var options = {
   host: 'xxxxxx',
   port: 80,
   path: xxxxxxx,
   method: 'GET'
};
var req = http.request(options, function (res) {
   console.log('FULL RESPONSE: ', res);
   res.setEncoding('utf8');
   res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
   });
});

req.on('error', function (e) {
   console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

我想在服务器(终端)中看到没有这种类型的[Object] [Array]和[Circular]的完整响应。

如果我把JSON.stringify放它会引发错误。

在这里详细说明我的意见:

res对象是一个流对象。 您想要的数据可能不在这里。 您将必须在res.on('data',function(chunk){...})部分中使用字符串/缓冲区构建数据。 数据可能会res.on('end',function(){...})多个块,因此您可能还想使用res.on('end',function(){...})来确定何时接收到所有数据。 之后,您可以处理您的字符串/缓冲区并使用JSON.parse来获取所需的数据

样本演示:

var req = http.request(options, function (res) {
   res.setEncoding('utf8');
   // Create a new Buffer
   var buffer = Buffer.from('');
   res.on('data', function (chunk) {
      // Each time a chunk comes through, write it to the buffer
      console.log('BODY: ' + chunk);
      buffer.write(chunk);
   });
   res.on('end',function(){
      // The request is complete here, try to get the JSON string and parse it
      // TODO: handle possible parse exception
      var data = JSON.parse(buffer.toString('utf8'));
   });
});

我建议使用像node-fetchrequest这样的库,该库允许您使用回调/承诺并为您提取流。 在大多数情况下,这些方法都可以很好地起作用,除非您要做一些非常简单的事情

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM