Migrate '$.ajax' to vanilla JS 'XMLHttpRequest': how to process requesttoken

Hello Devs,

I am migrating my apps away from jQuery - mainly due to performance reasons.
the last leftover are the $.ajax requests

To make a request via XMLHttpRequest is not the issue, but NC is expecting the requesttoken.
Can you help me on how to derive this ans pass it along with the Request?

just found the following in an app: OC.requestToken

var xhr = new XMLHttpRequest();
xhr.open(‘GET’, OC.generateUrl(xxx));
xhr.setRequestHeader(‘requesttoken’, OC.requestToken);
xhr.setRequestHeader(‘OCS-APIREQUEST’, ‘true’);

do I miss anything?

Yes, OC.requestToken would be the way to go for that, so the snipped should work fine. In case you are using some kind of bundler for your vanilla js app you can also just use our https://github.com/nextcloud/nextcloud-auth npm package for obtaining the token.