Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl command showing as undefined with token in swagger UI.?

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 imageenter image description here

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.?

like image 596
mkHun Avatar asked Mar 27 '20 09:03

mkHun


1 Answers

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
}
like image 68
Zav Avatar answered Oct 05 '22 23:10

Zav