使用Node.js

时间:2017-05-10 00:39:09

标签: node.js rest stack-overflow stackexchange-api stackexchange

我使用api.stackexchange.com的node.js实现的一个工作代码已经开始返回400响应代码。

有人可以建议可能出错的地方吗?添加代码示例和响应对象转储。

仅供参考,GET电话适用于Chrome / Postman等。

 var zlib = require("zlib");
var https = require('https');
var url = "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&site=stackoverflow";
https.get(url, function(response) {
    if (response.statusCode == 200) {
        var gunzip = zlib.createGunzip();
        var jsonString = '';
        response.pipe(gunzip);
        gunzip.on('data', function(chunk) {
            jsonString += chunk;
        });
        gunzip.on('end', function() {
            parseJson(jsonString, bot, message);
        });
        gunzip.on('error', function(e) {
            console.log("Error while parsing SOF response");
            botErrorHandler(e, bot, message)
        });
    } else {
        console.log(response);
        console.log("Error - While parsing SOF response. Response.statusCode: " + response.statusCode);
    }
});

这是响应对象,这是很多文本,但希望有人能够弄清楚发生了什么。

{ _readableState: 
{ highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: false,
     endEmitted: false,
     reading: false,
     calledRead: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     objectMode: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: { end: [Function: responseOnEnd] },
  _maxListeners: 10,
  socket: 
   { _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: true,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Object],
        sslOutEnd: [Function],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     pair: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        server: undefined,
        _secureEstablished: true,
        _isServer: false,
        _encWriteState: true,
        _clearWriteState: true,
        _doneFlag: false,
        _destroying: false,
        credentials: [Object],
        _rejectUnauthorized: true,
        _requestCert: true,
        ssl: {},
        servername: false,
        cleartext: [Circular],
        encrypted: [Object],
        fd: undefined,
        npnProtocol: false },
     _pending: null,
     _pendingEncoding: '',
     _pendingCallback: null,
     _doneFlag: false,
     _retryAfterPartial: false,
     _halfRead: false,
     _sslOutCb: null,
     _resumingSession: false,
     _reading: true,
     _destroyed: false,
     _ended: false,
     _finished: false,
     _opposite: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     _buffer: 
      { isFull: false,
        pool: <Buffer 22 69 64 22 3a 22 55 32 45 31 50 32 4d 36 5a 22 2c 22 74 65 61 6d 5f 69 64 22 3a 22 54 30 47 39 41 4c 37 42 38 22 2c 22 6e 61 6d 65 22 3a 22 74 68 6b 68 ...>,
        offset: 3351011,
        remaining: 7134749 },
     _handle: { readStop: [Function], readStart: [Function] },
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        onend: null,
        destroyed: false,
        errorEmitted: false,
        bytesRead: 4247,
        _bytesDispatched: 715,
        _pendingData: null,
        _pendingEncoding: '',
        read: [Function],
        _consuming: true },
     encrypted: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     authorized: true,
     _controlReleased: true,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: {},
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /2.2/search?order=desc&sort=activity&site=stackoverflow HTTP/1.1\r\nHost: api.stackexchange.com\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/2.2/search?order=desc&sort=activity&site=stackoverflow',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData],
     onend: [Function: socketOnEnd],
     npnProtocol: false },
  connection: 
   { _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: true,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Object],
        sslOutEnd: [Function],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     pair: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        server: undefined,
        _secureEstablished: true,
        _isServer: false,
        _encWriteState: true,
        _clearWriteState: true,
        _doneFlag: false,
        _destroying: false,
        credentials: [Object],
        _rejectUnauthorized: true,
        _requestCert: true,
        ssl: {},
        servername: false,
        cleartext: [Circular],
        encrypted: [Object],
        fd: undefined,
        npnProtocol: false },
     _pending: null,
     _pendingEncoding: '',
     _pendingCallback: null,
     _doneFlag: false,
     _retryAfterPartial: false,
     _halfRead: false,
     _sslOutCb: null,
     _resumingSession: false,
     _reading: true,
     _destroyed: false,
     _ended: false,
     _finished: false,
     _opposite: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     _buffer: 
      { isFull: false,
        pool: <Buffer 22 69 64 22 3a 22 55 32 45 31 50 32 4d 36 5a 22 2c 22 74 65 61 6d 5f 69 64 22 3a 22 54 30 47 39 41 4c 37 42 38 22 2c 22 6e 61 6d 65 22 3a 22 74 68 6b 68 ...>,
        offset: 3351011,
        remaining: 7134749 },
     _handle: { readStop: [Function], readStart: [Function] },
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        onend: null,
        destroyed: false,
        errorEmitted: false,
        bytesRead: 4247,
        _bytesDispatched: 715,
        _pendingData: null,
        _pendingEncoding: '',
        read: [Function],
        _consuming: true },
     encrypted: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     authorized: true,
     _controlReleased: true,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: {},
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /2.2/search?order=desc&sort=activity&site=stackoverflow HTTP/1.1\r\nHost: api.stackexchange.com\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/2.2/search?order=desc&sort=activity&site=stackoverflow',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData],
     onend: [Function: socketOnEnd],
     npnProtocol: false },
  httpVersion: '1.1',
  complete: false,
  headers: 
   { 'cache-control': 'private',
     'content-type': 'application/json; charset=utf-8',
     'content-encoding': 'gzip',
     'access-control-allow-origin': '*',
     'access-control-allow-methods': 'GET, POST',
     'access-control-allow-credentials': 'false',
     'x-content-type-options': 'nosniff',
     date: 'Tue, 09 May 2017 23:48:05 GMT',
     'content-length': '128' },
  trailers: {},
  _pendings: [],
  _pendingIndex: 0,
  url: '',
  method: null,
  statusCode: 400,
  client: 
   { _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: true,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Object],
        sslOutEnd: [Function],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     pair: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        server: undefined,
        _secureEstablished: true,
        _isServer: false,
        _encWriteState: true,
        _clearWriteState: true,
        _doneFlag: false,
        _destroying: false,
        credentials: [Object],
        _rejectUnauthorized: true,
        _requestCert: true,
        ssl: {},
        servername: false,
        cleartext: [Circular],
        encrypted: [Object],
        fd: undefined,
        npnProtocol: false },
     _pending: null,
     _pendingEncoding: '',
     _pendingCallback: null,
     _doneFlag: false,
     _retryAfterPartial: false,
     _halfRead: false,
     _sslOutCb: null,
     _resumingSession: false,
     _reading: true,
     _destroyed: false,
     _ended: false,
     _finished: false,
     _opposite: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     _buffer: 
      { isFull: false,
        pool: <Buffer 22 69 64 22 3a 22 55 32 45 31 50 32 4d 36 5a 22 2c 22 74 65 61 6d 5f 69 64 22 3a 22 54 30 47 39 41 4c 37 42 38 22 2c 22 6e 61 6d 65 22 3a 22 74 68 6b 68 ...>,
        offset: 3351011,
        remaining: 7134749 },
     _handle: { readStop: [Function], readStart: [Function] },
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        onend: null,
        destroyed: false,
        errorEmitted: false,
        bytesRead: 4247,
        _bytesDispatched: 715,
        _pendingData: null,
        _pendingEncoding: '',
        read: [Function],
        _consuming: true },
     encrypted: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Circular],
        _buffer: [Object] },
     authorized: true,
     _controlReleased: true,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: {},
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /2.2/search?order=desc&sort=activity&site=stackoverflow HTTP/1.1\r\nHost: api.stackexchange.com\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/2.2/search?order=desc&sort=activity&site=stackoverflow',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData],
     onend: [Function: socketOnEnd],
     npnProtocol: false },
  _consuming: false,
  _dumped: false,
  httpVersionMajor: 1,
  httpVersionMinor: 1,
  upgrade: false,
  req: 
   { domain: null,
     _events: {},
     _maxListeners: 10,
     output: [],
     outputEncodings: [],
     writable: true,
     _last: true,
     chunkedEncoding: false,
     shouldKeepAlive: true,
     useChunkedEncodingByDefault: false,
     sendDate: false,
     _headerSent: true,
     _header: 'GET /2.2/search?order=desc&sort=activity&site=stackoverflow HTTP/1.1\r\nHost: api.stackexchange.com\r\nConnection: keep-alive\r\n\r\n',
     _hasBody: true,
     _trailer: '',
     finished: true,
     _hangupClose: false,
     socket: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        parser: [Object],
        _httpMessage: [Circular],
        ondata: [Function: socketOnData],
        onend: [Function: socketOnEnd],
        npnProtocol: false },
     connection: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: false,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        parser: [Object],
        _httpMessage: [Circular],
        ondata: [Function: socketOnData],
        onend: [Function: socketOnEnd],
        npnProtocol: false },
     agent: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        options: {},
        requests: {},
        sockets: [Object],
        maxSockets: 5,
        createConnection: [Function: createConnection] },
     socketPath: undefined,
     method: 'GET',
     path: '/2.2/search?order=desc&sort=activity&site=stackoverflow',
     _headers: { host: 'api.stackexchange.com' },
     _headerNames: { host: 'Host' },
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Object],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     res: [Circular] } }
Error - While parsing SOF response. Response.statusCode: 400

0 个答案:

没有答案