0

我正在遵循以下示例:

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);
});

出于所有意图和目的,我的代码基本上是相同的。

根据 API 响应正文应该是 Message 类型,但我得到的是以下内容:

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
  }
}

我不确定如何处理这些数据以将其读取为所需的消息类型,如 API 文档中所引用。任何帮助将不胜感激!

供参考:https ://developers.google.com/chat/reference/rest/v1/spaces/webhooks

4

1 回答 1

0

解决方案非常简单。我忘记了,为了阅读此处fetch指定的内容,我需要调用

const resJson = await response.json()
于 2021-08-24T16:25:33.307 回答