After some experimenting if appears that Swagger uses jquery for OPTIONS requests whilst it uses superagent client-side HTTP request library for other types of requests such as GET, POST etc...
Thus, to add a custom header for every request in Swagger-UI, use the following code:
$(document).ready(function () {
// Intercept jquery ajax request
$.ajaxSetup({
beforeSend: function (xhr, settings) {
// Add the header to the Ajax request
xhr.setRequestHeader("Authorization", getHeader());
},
// Disable caching of AJAX responses
cache: false
});
// Intercept superagent request
window.swaggerUi.options["requestInterceptor"] = function () {
// Add the header to the request
this.headers.Authorization = getHeader();
return this;
};
});
function getHeader() {
var header = ... some code to calculate or generate a header ...
return header
}