如何将标题信息放入swagger json [英] How to put header information in 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/describing-request-body/
在我的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标头信息?我没能在庞大的文档中看到这些信息.
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
头由 consumps
定义,并且 produces
关键字.可以在操作级别或规范的根级别上指定它们.
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
标头是使用 securityDefinitions
和 security
关键字定义的.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.
例如,如果某个操作发布JSON并使用基本身份验证,则可以将其描述如下:
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
这篇关于如何将标题信息放入swagger json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!