如何将标头信息放入swagger json中 [英] How to put header information in swagger json

查看:16
本文介绍了如何将标头信息放入swagger json中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

https://swagger.io/docs/specification/2-0/描述请求体/

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

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

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

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.

推荐答案

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

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.

Authorization 标头是使用 securityDefinitionssecurity 关键字定义的.OpenAPI/Swagger 2.0 支持基本身份验证、API 密钥和 OAuth 2.

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

其他标头可以定义为in: 标题 参数.

Other headers can be defined as in: header parameters.

比如一个操作POST JSON,使用Basic auth,你可以这样描述:

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

相关文档:
MIME 类型
身份验证
描述参数

这篇关于如何将标头信息放入swagger json中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆