How to add Accept, Authorization or Content-Type in OpenAPI 3.0?

Viewed 214

My spec is as below.

/path:
 /user:
  get:
    parameters:
     - name: Authorization
       in: header
       required: true
       schema:
        type: string

Problem is that it is giving me the below warning. I get the same warning if I add Content-Type or Accept header.

Header parameters named Authorization are ignored. Use securitySchemes and security to define the Authorization

I tried the below but I don't see Authorization header added in the request. I am using https://editor.swagger.io to create the spec.

/path:
 /user:
  get:
    parameters:
     - name: Authorization
       in: header
       required: true
       schema:
        type: string
    security:
     - my_auth: []

components:
 securitySchemes:
  my_auth:
   type: http
   scheme: bearer
   bearerFormat: JWT

Any help is appreciated. Thanks !!

1 Answers

In the request parameters, there are operation's specific parameters.

The general purpose HTTP headers aren't defined here because:

  • Content-Type is defined by the request body content. If there are multiple content types, the consumer has to choose and set Content-Type accordingly.
  • Accept is similar; it only relates to the response message.
  • For security, we do not describe the Authorization header but instead define the security scheme (see docs for more).

You may use the description property to explain how to use these headers with your API. However, if your API follows standards, it should not be necessary.

Once you have added the security schema to your API definition, you can use the Authorization function of Swagger Editor. So, you will add your token and trigger "Try it out." Swagger will populate the Authorization header; see the attached screenshot.

Swagger Authorization

Related