我的swagger.json
文件(localhost:8000/rest/swagger.json
)需要AuthType
标头才能访问它的端点。如何在向swagger.json
文件发出初始请求时让Swagger UI添加它?
到目前为止我已尝试过:
$(function () {
var token = 'xxx';
window.swaggerUi = new SwaggerUi({
url: "http://" + location.host + "/rest/swagger.json",
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function (swaggerApi, swaggerUi) {
var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("AuthToken", token, "header");
window.swaggerUi.api.clientAuthorizations.add("AuthToken", apiKeyAuth);
$('pre code').each(function (i, e) {
hljs.highlightBlock(e)
});
},
docExpansion: "none",
apisSorter: "alpha",
showRequestHeaders: false
});
swaggerUi.load();
});
但是,当我打开Chrome开发者工具并查看向localhost:8000/rest/swagger.json
发出的请求时,它没有AuthType
标头并且有401 Unauthorized
响应。
注意:onComplete
函数看起来似乎没有被调用(我猜测它之前请求swagger.json
通常会被调用,因此它会落入{onFailure
1}}在请求失败时阻止)
答案 0 :(得分:3)
我会告诉你我的情景。老实说,swagger的READ-ME不符合标准(这是我的看法)。他们没有提到有关向网址请求添加标头的任何内容。
我的用例是我必须调用我的API来获取 JSON响应。(我的API受 login_required 装饰器保护,这需要 xcsrf-token 将在标题中发送)
在您的情况下,您的 localhost:8000 / rest / swagger.json 类似于我的API。
Click here for Swagger-UI example
使用AJAX请求调用您的API或 的本地主机:8000 /休息/ swagger.json 强>
现在,在您的成功通话中, JSON 必须等同于“spec”。
const ui = SwaggerUIBundle({
spec: {},// put JSON response here.
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
// yay ES6 modules ↘
Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
window.foo = function(spec){
const ui = SwaggerUIBundle({
spec: spec,
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
// yay ES6 modules ↘
Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
window.onload = function() {
function apiCall(uri, data, methodType) {
const csrftoken = getCookie('csrftoken');
//Add header to the URL .
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!this.crossDomain) {
xhr.setRequestHeader('X-CSRFToken', csrftoken);
}
}
});
$.ajax({
url: uri,
method: methodType,
contentType: 'application/json',
data: data,
success: function (response) {
var script = document.createElement('script');
// Calling the function which instantiates swaggerbundle object
foo(response)
},
error: function (error) {
}
});
}
// You can write your own getCookie function .
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
let cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
//['GET','POST']
apiCall('http://localhost:8000/rest/swagger.json',{},'GET');
}
window.foo = function(spec){
const ui = SwaggerUIBundle({
spec: spec,
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
// yay ES6 modules ↘
Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
window.onload = function() {
function apiCall(uri, data, methodType) {
const csrftoken = getCookie('csrftoken');
//Add header to the URL .
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!this.crossDomain) {
xhr.setRequestHeader('X-CSRFToken', csrftoken);
}
}
});
$.ajax({
url: uri,
method: methodType,
contentType: 'application/json',
data: data,
success: function (response) {
var script = document.createElement('script');
// Calling the function which instantiates swaggerbundle object
foo(response)
},
error: function (error) {
}
});
}
// You can write your own getCookie function .
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
let cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
//['GET','POST']
apiCall('http://localhost:8000/rest/swagger.json',{},'GET');
}
答案 1 :(得分:1)
在您的示例中,您有window.swaggerUi
,但尚未将swaggerApi
变量分配给window
对象。
尝试:
onComplete: function (swaggerApi, swaggerUi) {
window.swaggerApi = swaggerApi;
var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("AuthToken", token, "header");
window.swaggerUi.api.clientAuthorizations.add("AuthToken", apiKeyAuth);
$('pre code').each(function (i, e) {
hljs.highlightBlock(e)
});
}
第二:
虽然浏览器可能能够找到正确的swaggerUi
变量尝试加载:
window.swaggerUi.load();
而不是:
swaggerUi.load();