I am integrating the swagger UI in my project. I need to pass the token to make a request.
const mytoken = "heareismytoken";
const ui = SwaggerUIBundle({
url: "/swagger/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
requestInterceptor: function (req) {
var key = mytoken;
if (key && key.trim() !== "") {
req.headers.Authorization = 'Bearer ' + key;
console.log('Authorized from authKey');
}
},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
});
With the above code I am getting the successful response but the problem is curl command is showing as undefined like below image
If I removed the following part of code
/*
requestInterceptor: function (req) {
var key = mytoken;
if (key && key.trim() !== "") {
req.headers.Authorization = 'Bearer ' + key;
console.log('Authorized from authKey');
}
}, */
the curl command is showing but the response is throwing the authentication error.
I don't know exactly where I am missing it. How to the show both CURL command and the Response.?
According to the documentation of Swagger UI:
requestInterceptor:
Function=(a => a)
. MUST be a function. Function to intercept remote definition, "Try it out", and OAuth 2.0 requests. Accepts one argument requestInterceptor(request) and must return the modified request, or a Promise that resolves to the modified request.
In provided code return statement is missing. Correct code will be:
requestInterceptor: function (req) {
var key = mytoken;
if (key && key.trim() !== "") {
req.headers.Authorization = 'Bearer ' + key;
console.log('Authorized from authKey');
}
return req; // <--- This line was added
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With