卷曲命令显示为 undefined with token in swagger UI.?

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

我正在将 swagger UI 集成到我的项目中。我需要传递令牌才能发出请求。

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",
});

使用上面的代码,我得到了成功的响应,但问题是 curl 命令显示为未定义,如下图所示

如果我删除了以下部分代码

    /* 
    requestInterceptor: function (req) {
        var key = mytoken;

        if (key && key.trim() !== "") {
            req.headers.Authorization = 'Bearer ' + key;
            console.log('Authorized from authKey');
        }
    }, */

正在显示 curl 命令,但响应抛出身份验证错误。

我不知道我到底错过了什么。如何同时显示 CURL 命令和响应?

根据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.

在提供的代码中 return 语句缺失。正确的代码将是:

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
}