我尝试设置Swagger> = 2.0但我无法通过单个请求发送多个标头。那是我的index.html js代码:
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://localhost:9033/api/swagger.json";
}
window.swaggerUi = new SwaggerUi({
url: url,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi){
if(typeof initOAuth == "function") {
initOAuth({
clientId: "your-client-id",
realm: "your-realms",
appName: "your-app-name"
});
}
$('pre code').each(function(i, e) {
hljs.highlightBlock(e)
});
addApiKeyAuthorization();
},
onFailure: function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion: "none",
apisSorter: "alpha",
showRequestHeaders: true
});
function addApiKeyAuthorization(){
var key = encodeURIComponent($('#input_apiKey')[0].value);
if(key && key.trim() != "") {
window.swaggerUi.api.clientAuthorizations.add("X-My-Header", new SwaggerClient.ApiKeyAuthorization("X-My-Header", "test value", "header"));
window.swaggerUi.api.clientAuthorizations.add("Authorization", new SwaggerClient.ApiKeyAuthorization("Authorization", key, "header"));
log("added key " + key);
}
}
$('#input_apiKey').change(addApiKeyAuthorization);
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/*
var apiKey = "myApiKeyXXXX123456789";
$('#input_apiKey').val(apiKey);
*/
window.swaggerUi.load();
function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
正如您所看到的,我添加了两个标题,但只有第一个标题随请求一起发送。如果我反转它们,那么另一个将被发送,但我从来没有设法发送它们。文档很简单,并说:
请注意!您可以在单个请求中传递多个标头参数,只需为它们使用唯一的名称(在上面的示例中使用了密钥)。
要明确的是,两个标头都会添加到Access-Control-Allow-Headers中,并且两者都可以由服务器单独恢复,这种情况是我无法将它们一起发送。
问题: 有谁知道如何处理?
(还有一个重要的假设,这些标题对我所有的api来说都是全局的,所以我想把它们直接放在swagger ui而不是记录每个api方法。)