使用 POST/PUT/PATCH 请求将负载数据发送到 Sentry.io

Send payload data with POST/PUT/PATCH requests to Sentry.io

我正在尝试将负载数据添加到发送至 Sentry.io 的面包屑中。

看起来就是这样。

我找到了如何添加响应。

const sentryConfig: BrowserOptions = {
  beforeBreadcrumb: (breadcrumb, hint) => {
    if (breadcrumb.category === 'xhr') {
      // hint.xhr is a whole XHR object that you can use to modify breadcrumb
      breadcrumb.data = (hint.xhr as XMLHttpRequest).response;
    }

    return breadcrumb;
  }
};

但我似乎找不到添加有效负载的方法。 XMLHttpRequest 没有此信息。

我认为没有办法从 xhr 请求中获取负载。在创建 xhr 请求时,我只会 console.log() 与有效载荷一起使用。它不会是同一个面包屑的一部分,但您会在 Sentry 中看到它。

可以通过这种方式从您的 xhr 获取正文(有效负载数据):

beforeBreadcrumb: (breadcrumb, hint) => {
 if (breadcrumb.category === 'xhr') {
   const data = {
     requestBody: hint.xhr.__sentry_xhr__.body,
     response: hint.xhr.response,
     responseUrl: hint.xhr.responseURL
   }
   return { ...breadcrumb, data }
 }
 return breadcrumb
}

这是添加它的PR