我正在使用 swagger servicestack但我从我的/resources URL 收到 401 未经授权的错误,因为它需要 API key 。
除非我弄错了,according to the documentation从我的 html 页面初始化 Swagger 时,我应该将 supportHeaderParams 设置为 true,以及 JSON 参数中的 apiKeyName 和 apiKey 值。
然后,我希望在 http 请求 header 中看到我的 API key ,但它仍然被附加到 URL,而不是 header 集合中。
以下是在我的 HTML 页面中初始化 Swagger 的代码:
window.swaggerUi = new SwaggerUi({
discoveryUrl: "http://pathtomyservice.com/resources",
headers: { "testheader" : "123" },
apiKey: "123",
apiKeyName: "Api-Key",
dom_id:"swagger-ui-container",
supportHeaderParams: true,
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
if(console) {
console.log("Loaded SwaggerUI");
console.log(swaggerApi);
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
if(console) {
console.log("Unable to Load SwaggerUI");
console.log(data);
}
},
docExpansion: "none"
});
不幸的是,我根本没有得到任何 header ,没有“Api-Key”或“testheader”。
最佳答案
我认为这可能是 swagger ui 中的一个错误。
作为解决方法,我在 swagger index.html 文件中添加了以下内容。
$(function () {
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
jqXHR.setRequestHeader("YourApiKeyHeader", $("#input_apiKey").val());
}
});
});
希望这有帮助,
关于javascript - 如何让 Swagger 将 API key 作为 http 而不是在 URL 中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16899515/