【问题标题】:Get body of request within Axios error object在 Axios 错误对象中获取请求正文
【发布时间】:2022-11-01 21:01:43
【问题描述】:

有据可查的是,发生错误时,Axios Error 对象将包含响应和请求。

在失败的 POST 上,我想访问请求正文以进行日志记录,但我不知道如何

try {
  await axios.post('http://example.com', { test: data }, { 'content-type': 'application/json' })
} catch (error) {
  console.log(error.response.data)
  console.log(error.request.data)      // Undefined, should be '{ test: data }'
}

【问题讨论】:

    标签: javascript node.js axios


    【解决方案1】:

    正文在 Axios 配置属性中

    try {
      await axios.post('http://example.com', { test: data }, { 'content-type': 'application/json' })
    } catch (error) {
      console.log(error.response.data)
      console.log(error.config.data)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2021-12-05
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多