How to put header information in swagger json

北城以北 提交于 2021-02-17 02:00:17

问题


I followed the following link from swagger documentation to create swagger json for my rest api.

https://swagger.io/docs/specification/2-0/describing-request-body/

In my rest api, I have request body and http headers like Content-Type and Authorization that go along with the service request.

I was wondering if there is a way to include request body and http header information in the swagger json ? I don't see that information in the swagger docs.


回答1:


The Content-Type header of requests and responses is defined by the consumes and produces keywords, respectively. They can be specified on the operation level or on the root level of the spec.

The Authorization header is defined using the securityDefinitions and security keywords. OpenAPI/Swagger 2.0 supports Basic authentication, API keys and OAuth 2.

Other headers can be defined as in: header parameters.

For example, if an operation POSTs JSON and uses Basic auth, you can describe it as follows:

swagger: '2.0'
...

securityDefinitions:   # Authorization, part 1
  basicAuth:
    type: basic

paths:
  /something:
    post:
      summary: POST some JSON
      consumes:
        - application/json  # Request Content-Type
      produces:
        - application/json  # Response Content-Type
      security:
        - basicAuth: []     # Authorization, part 2
      parameters:
        - in: body
          name: body
          required: true
          schema:
            $ref: '#/definitions/Something'
      responses:
        200:
          description: OK

Relevant documentation:
MIME Types
Authentication
Describing Parameters



来源:https://stackoverflow.com/questions/47288833/how-to-put-header-information-in-swagger-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!