如何将标题信息放在swagger json中

时间:2017-11-14 14:57:15

标签: json rest api swagger

我按照swagger文档中的以下链接为我的其余api创建了swagger json。

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

在我的api api中,我已经请求了与内容类型和授权类似的主体和http标头,以及服务请求。

我想知道是否有办法在swagger json中包含请求正文和http头信息?我在swagger文档中看不到这些信息。

1 个答案:

答案 0 :(得分:0)

请求和响应的Content-Type标头分别由consumesproduces关键字定义。它们可以在操作级别或规范的根级别指定。

使用AuthorizationsecurityDefinitions关键字定义security标头。 OpenAPI / Swagger 2.0支持基本身份验证,API密钥和OAuth 2。

其他标头可以定义为in: header parameters

例如,如果操作POST JSON并使用Basic auth,您可以按如下方式对其进行描述:

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

相关文件:
MIME Types
Authentication
Describing Parameters