如何获取Swagger以HTTP而不是URL的形式发送API密钥 [英] How to get Swagger to send API key as a http instead of in the URL
问题描述
我正在将swagger与 servicestack 一起使用,但是我从/resources URL中收到401未经授权的错误因为它需要一个API密钥.
I am using swagger with servicestack but I am getting a 401 unauthorised error from my /resources URL becuase it requires an API key.
除非我弄错了,否则根据文档,我应该将 supportHeaderParams 设置为true从我的html页面初始化Swagger时,以及JSON参数中的 apiKeyName 和 apiKey 值.
Unless I'm mistaken, according to the documentation I should set supportHeaderParams to true as well as the apiKeyName and apiKey value in the JSON parameters when initializing Swagger from my html page.
然后我期望在http请求标头中看到我的API密钥,但是它仍被附加到URL而不是标头集合中.
I was then expecting to see my API key in the http request headers, but it is still being appended to the URL and not in the headers collection.
以下是在我的HTML页面中初始化Swagger的代码:
Here is the code that initialises Swagger in my HTML page:
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"
});
不幸的是,我根本没有标题,也没有'Api-Key'或'testheader'.
Unfortunately I get no headers at all, no 'Api-Key' or 'testheader'.
推荐答案
我认为这可能是swagger ui中的错误.
I think that it might be a bug in swagger ui.
作为一种解决方法,我在swagger index.html文件中添加了以下内容.
As a workaround, I added the following in in the swagger index.html file.
$(function () {
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
jqXHR.setRequestHeader("YourApiKeyHeader", $("#input_apiKey").val());
}
});
});
希望这会有所帮助,
这篇关于如何获取Swagger以HTTP而不是URL的形式发送API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!