简体   繁体   English

Swagger ui url带参数

[英]Swagger ui url with parameters

How to pass base url in the form http://localhost:3000/resources/api/?key=aslkdajd1323121lklakskdl to swagger ui ? 如何将基本URL以http://localhost:3000/resources/api/?key=aslkdajd1323121lklakskdl给swagger ui?

I was able to access http://localhost:3000/resources/api but when I add auth filter and pass key, it says, Unauthorized . 我能够访问http://localhost:3000/resources/api但是当我添加auth过滤器并传递密钥时,它说, Unauthorized

Using swagger 1.X 使用swagger 1.X

Pre-populating the parameter through apiKeyauthorization in index.html did not help, but when I type in the key in UI, it worked. 通过index.html中的apiKeyauthorization预先填充参数没有帮助,但是当我在UI中键入键时,它起作用了。 Unable to understand the reason for this. 无法理解其原因。 Hope someone can help me make sense out of it. 希望有人可以帮助我理解它。

Try this swagger 2.0 file (use http://studio.restlet.com to downgrade to version 1.2) : 试试这个swagger 2.0文件(使用http://studio.restlet.com降级到1.2版):

{
    "swagger": "2.0",
    "info": {
        "version": "0.0.1",
        "title": "Todo App"
    },
    "host": "localhost:3000",
    "schemes": [
    "http"
    ],
    "paths": {
        "/resources/api": {
            "post": {
                "parameters": [
                    {
                        "name": "key",
                        "in": "query",
                        "description": "key",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Successful response"
                    }
                }
            }
        }
    }
}

I was able to solve this by adding window.authorizations.add("key", new ApiKeyAuthorization("key", yourKeyValue, "query")); 我能够通过添加window.authorizations.add("key", new ApiKeyAuthorization("key", yourKeyValue, "query"));来解决这个问题window.authorizations.add("key", new ApiKeyAuthorization("key", yourKeyValue, "query"));

in the SwaggerUI constructor function window.swaggerUi = new SwaggerUi({ . . . 在SwaggerUI构造函数window.swaggerUi = new SwaggerUi({ . . .

right before the statement window.swaggerUi.load() 就在语句window.swaggerUi.load()

You just need get the parameter from the url with javscript. 您只需要使用javscript从url获取参数。 In the file "index.html", under swagger-ui/dist folder, add something like this to get your key: 在文件“index.html”中,在swagger-ui / dist文件夹下,添加这样的内容以获取密钥:

var key = window.location.search.match(/key=([^&]+)/);

You can see a simple example in my GIST . 你可以在我的GIST中看到一个简单的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM