【问题标题】:Unable to parse Response message using node-fetch无法使用 node-fetch 解析响应消息
【发布时间】:2021-06-02 13:45:33
【问题描述】:

我的目标是阅读响应中返回的消息。我正在使用 node-fetch 并且响应被压缩。这是我目前所拥有的:

  const response = await fetch(config.url, {
            method: 'POST',
            body: request,
            headers: {'Content-Type': 'application/json; charset=UTF-8', 'x-tn-api_key':config.key, 'x-tn-api_signature':generateAPISignature()}
          })

    let deserializedResponse = await response.json()

这是反序列化响应的样子:

{
  "timestamp": "2021-03-03T22:34:37.362+0000",
  "status": 400,
  "error": "Bad Request",
  "message": "JSON parse error: Cannot deserialize instance of `xyz` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `xyz` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
  "path": "/a/v1/events"
}

message字段出现json解析错误,如何读取message字段?本质上,如果调用失败,我想记录消息以进行调试。

更新:我将请求对象硬编码为某个静态对象,但仍然遇到相同的错误。当我将相同的静态对象复制粘贴到 Postman 时,它工作正常。有谁知道为什么 body:request 被读取为请求数组?似乎这是错误的原因,因为它期望请求中有一个对象,而不是一个对象数组

【问题讨论】:

    标签: node.js typescript node-fetch


    【解决方案1】:

    更换

        body: request,
    

    body: JSON.stringify(request),
    

    好像解决了

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      相关资源
      最近更新 更多