Swagger ui url with parameters

8.3k Views Asked by At

How to pass base url in the form http://localhost:3000/resources/api/?key=aslkdajd1323121lklakskdl to swagger ui ?

I was able to access http://localhost:3000/resources/api but when I add auth filter and pass key, it says, Unauthorized.

Using 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. Unable to understand the reason for this. Hope someone can help me make sense out of it.

3

There are 3 best solutions below

2
On

Try this swagger 2.0 file (use http://studio.restlet.com to downgrade to version 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"
                    }
                }
            }
        }
    }
}
1
On

You just need get the parameter from the url with javscript. In the file "index.html", under swagger-ui/dist folder, add something like this to get your key:

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

You can see a simple example in my GIST.

0
On

I was able to solve this by adding window.authorizations.add("key", new ApiKeyAuthorization("key", yourKeyValue, "query"));

in the SwaggerUI constructor function window.swaggerUi = new SwaggerUi({ . . .

right before the statement window.swaggerUi.load()