add X-XSRF-TOKEN to the request header of sendBeacon()

306 Views Asked by At

I have an Angular application and to send analytics data to the server I defined the service that used the navigator.sendBeacon(). I wanted to add X-XSRF-TOKEN into the request header of navigator.sendBeacon(). But as you know, it's not possible, or at least I don't know how to do it. I know I can do it by fetching API but I am curious, is it a better way to do this?

fetch('url', {
  keepalive: true,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-XSRF-TOKEN': 'sample-token',
  },
  body: JSON.stringify(sample-data),
});
0

There are 0 best solutions below