简体   繁体   中英

How to get response data from google-chat webhook?

I'm following the following example:

const fetch = require('node-fetch');

const webhookURL = '<INCOMING-WEBHOOK-URL>';

const data = JSON.stringify({
  'text': 'Hello from a Node script!',
});

fetch(webhookURL, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
  },
  body: data,
}).then((response) => {
  console.log(response);
});

For all intents and purposes, my code is essentially the same.

According to API response body should be of type Message but instead I'm getting the following:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 00 48 02 15 01 00 00 00 05 00 04 00 04 00 04 00 38 2f 01 15 01 00 00 00 30 46 01 15 01 00 00 00 1e 1e ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _maxOutputLength: 4294967295,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false,
      [Symbol(kTransformState)]: [Object],
      [Symbol(kError)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: <my webhook url>,
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

I'm not sure how to process this data to read it as the desired Message type, as referenced in the API docs. Any help would be greatly appreciated!

For reference: https://developers.google.com/chat/reference/rest/v1/spaces/webhooks

Solution was remarkably simple. I forgot that in order to read the contents of fetch as specified here I need to call

const resJson = await response.json()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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