1

I need to add bearer token in each API which has documented in swagger editor. But as per my code its not working as expected. I am explaining my code below.

swagger: "2.0"
info:
  description: "This is a sample API documentation of DCI project."
  version: "1.0.0"
  title: "Swagger API docs for DCI"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "**.**.**.**"
basePath: "/bpa/api/v1.0/workflow/process-definition/"
tags:
- name: "DCI"
  description: "API to start the workflow of DCI."
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
schemes:
- "https"
- "http"
paths:
  /DCI_PORT_MGMT/start:
    post:
      tags:
      - "Port Allocation"
      summary: "Add a new port"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Start the port allocation workflow."
        required: true
        schema:
          type: object
      responses:
        "405":
          description: "Invalid input"
      security:
      - petstore_auth:
        - "write:pets"
        - "read:pets"
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

Here I need to add bearer token in header of each API. as per my code when I am executing the API its not asking to add any token . If somebody can help me it will be great help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
subhra_user
  • 439
  • 5
  • 19

1 Answers1

1

According to swagger docs and other answers you just need to have the security definition then use it in one or more of the paths. For example

swagger: "2.0"
info:
  description: "This is a sample API documentation of DCI project."
  version: "1.0.0"
  title: "Swagger API docs for DCI"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "**.**.**.**"
basePath: "/bpa/api/v1.0/workflow/process-definition/"
tags:
- name: "DCI"
  description: "API to start the workflow of DCI."
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
schemes:
- "https"
- "http"
paths:
  /DCI_PORT_MGMT/start:
    post:
      security:
        - Bearer: []
      tags:
      - "Port Allocation"
      summary: "Add a new port"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Start the port allocation workflow."
        required: true
        schema:
          type: object
      responses:
        "405":
          description: "Invalid input"
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

If you test in swagger editor there is a authorize button (top right) that should allow you to enter credentials for this header. You can test the request/ execute it and you will see that a authorization header will be added.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
User7007
  • 331
  • 3
  • 14