cancel
Showing results for 
Search instead for 
Did you mean: 

JTW token is not getting passed in Swagger-ui

SOLVED
Highlighted
New Contributor

JTW token is not getting passed in Swagger-ui

JWT token is not getting passed under 'Authorization' feild.

Here's the full post https://stackoverflow.com/questions/63119252/jtw-token-is-not-getting-passed-in-swagger-ui

Tags (3)
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Moderator

Re: JTW token is not getting passed in Swagger-ui

Hi @rahuldwivedi,

Make sure your API definition includes the "security" key on the root level or in operations. For example:

swagger: '2.0'
securityDefinitions:
  ApiKeyAuth:
    type: apiKey
    in: header
    name: Authorization
security:  # <-------------
  - ApiKeyAuth: []

Check out our OpenAPI 2.0: API Keys guide for more examples.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

Highlighted
Moderator

Re: JTW token is not getting passed in Swagger-ui

Replace

    "security":{
        "ApiKeyAuth": []
    },

with

  "security": [
    {
      "ApiKeyAuth": []
    }
  ],

 

Swagger Editor is a YAML editor, but you can also paste JSON and import JSON files. JSON will be automatically converted to YAML. Then after you fixed the errors you can use the "File > Convert and save as JSON" command to export back to JSON.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

6 REPLIES 6
Highlighted
Moderator

Re: JTW token is not getting passed in Swagger-ui

Hi @rahuldwivedi,

Make sure your API definition includes the "security" key on the root level or in operations. For example:

swagger: '2.0'
securityDefinitions:
  ApiKeyAuth:
    type: apiKey
    in: header
    name: Authorization
security:  # <-------------
  - ApiKeyAuth: []

Check out our OpenAPI 2.0: API Keys guide for more examples.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

Highlighted
New Contributor

Re: JTW token is not getting passed in Swagger-ui

Hey @HKosova  thanks for the reply. Looks like I'm missing something more here.  When I added the field and hit execute, the UI keeps on showing the loader, this is what I'm getting on console.:

 

system.js:464 TypeError: f.forEach is not a function
at t.request (build-request.js:81)
at fn (build-request.js:15)
at Object.gn [as buildRequest] (index.js:249)
at actions.js:428
at Object.dispatch (utils.js:195)
at dispatch (<anonymous>:1:28545)
at redux.js:477
at wrap-actions.js:33
at Object.r (system.js:174)
at Object.executeRequest (system.js:461)

Highlighted
Moderator

Re: JTW token is not getting passed in Swagger-ui

Paste your API definition into https://editor.swagger.io to check if there are any syntax errors. If the editor shows any errors, you'll need to fix them before using "try it out".

 

If this does not help, can you attach your API definition (or a minimal example) here so that we could reproduce the issue?


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
New Contributor

Re: JTW token is not getting passed in Swagger-ui

@HKosova  I looked into that editor, but it looks like this is using .yaml  only. I pasted my swagger.json on the editor and it showed an error, "security should be an array".  Here's the complete code. How should I modify the security field?
 
{
    "swagger": "2.0",
    "info": {
      "version": "1.0.0",
      "title": "Magic 8 ball App API",
      "description": "Find out how your APIs work",
      "license": {
        "name": "MIT",
        "url": "https://opensource.org/licenses/MIT"
      }
    },
    "host": "localhost:3000",
    "tags": [
      {
        "name": "Users",
        "description": "API for users in the system"
      }

 

    ],
    "schemes": [
      "http",
      "https"
    ],
    "consumes": [
      "application/json"
    ],
    "produces": [
      "application/json"
    ],
    "securityDefinitions": {
        "ApiKeyAuth":{
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        }
    },
    "security":{
        "ApiKeyAuth": []
    },
    "paths": {
      "/api/eight-ball/user/login": {
        "post": {
          "summary": "Login user",
          "tags": [
            "Users"
          ],
          "description": "Login user in system",
          "parameters": [
            {
              "name": "user",
              "in": "body",
              "description": "Login user",
              "schema": {
                "$ref": "#/definitions/User"
              }
            }
          ],
          "produces": [
            "application/json"
          ],
          "responses": {
            "200": {
              "description": "Login Success",
              "schema": {
                "$ref": "#/definitions/User"
              }
            },
            "401":{
              "description": "Login details are not valid!!"
            },
            "404":{
              "description": "Email is not registered!"
            },
            "500":{
              "description": "User login failed!!"
            }
          }
        }
      }
    },

 

    "definitions": {

 

      "User": {
        "properties": {
          "email": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        }
      },
      "userEmail":{
        "properties": {
          "email": {
            "type": "string"
          }
        }
      }
    }
  }
 
 
The relevant code at node js:


const swaggerDocument = require('./swagger.json');
const swaggerUi = require('swagger-ui-express');
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
Highlighted
Moderator

Re: JTW token is not getting passed in Swagger-ui

Replace

    "security":{
        "ApiKeyAuth": []
    },

with

  "security": [
    {
      "ApiKeyAuth": []
    }
  ],

 

Swagger Editor is a YAML editor, but you can also paste JSON and import JSON files. JSON will be automatically converted to YAML. Then after you fixed the errors you can use the "File > Convert and save as JSON" command to export back to JSON.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

Highlighted
New Contributor

Re: JTW token is not getting passed in Swagger-ui

@HKosova  Yay! Finally solved it. Thank you so much. So here's what I did.

 

I imported my swagger.json to this editor you linked so it automatically converted to .yml and then added

security:
- ApiKeyAuth: []

 

as per your suggestion. Finally downloaded it by converting to .json. Pasted the code in my project and everything worked. Thanks again.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors
Top Kudoed Posts