【问题标题】:How to put header information in swagger json如何将标头信息放入swagger json中
【发布时间】:2017-11-14 14:57:14
【问题描述】:

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

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

在我的 rest api 中,我有请求正文和 http 标头,例如 Content-Type 和 Authorization 与服务请求一起使用。

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

【问题讨论】:

    标签: json rest api swagger


    【解决方案1】:

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

    Authorization 标头是使用 securityDefinitionssecurity 关键字定义的。 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

    【讨论】:

    • 有没有办法在 swagger 规范 2.0 中使用不记名令牌指定授权标头?
    • @Hary:假设您的意思是独立的不记名令牌(不是 OAuth 2 流程的一部分)——在 2.0 中,您会将其定义为 API key,而单词 Bearer 被认为是核心价值。最新版本 OpenAPI 3.0 原生支持 Bearer 身份验证。详情请见this question
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2023-02-17
    • 1970-01-01
    相关资源
    最近更新 更多