下面是*.yml接口,我想用Content-type: text/plain来响应数据,但现在总是返回application/json。
/order:
post:
tags:
- order
summary: order
operationId: PostOrder
parameters:
requestBody:
description: Order result
content:
application/json:
schema:
type: object
properties:
openReq:
type: string
example: 'test'
responses:
200:
description: Customer order receive successed
headers: {}
content:
application/json:
schema:
type: string
text/plain:
schema:
type: string使用swagger生成器生成的相同响应代码:
def post_order(platform, open_req=None): # noqa: E501
"""order
"""
return 'do some magic!'响应头始终为content-type: application/json
responses:
200:
description: Customer order receive successed
headers: {}
content:
application/json:
schema:
type: string
text/plain:
schema:
type: string此响应标头始终为content-type: text/plain; charset=utf-8
responses:
200:
description: Customer order receive successed
headers: {}
content:
# application/json:
# schema:
# type: string
text/plain:
schema:
type: string是否可以在post_order函数中设置响应头部内容类型
发布于 2020-10-08 02:04:27
也许你混淆了Swagger Docs接口和实际实现,你的文档是正确的,这意味着响应200 OK,可以作为application/json或text/plain返回。返回哪一个完全取决于终结点的实现。如果您的端点只返回application/json,那么您将永远不会收到text/plain,这不是Swagger/OpenApi的任务。
发布于 2021-01-28 08:21:29
如果希望函数动态决定返回哪种内容类型,则必须按照documentation中的说明显式设置标头。
两种方法之一是返回内容的元组、返回代码和标题字典,如下所示:
def post_order(platform, open_req=None):
"""order
"""
return 'do some magic!', 200, {'content-type': 'text/plain'}第二种方法是显式创建一个响应对象并返回:
from connexion.lifecycle import ConnexionResponse
def post_order(platform, open_req=None):
"""order
"""
return ConnexionResponse(
status_code=200,
content_type='text/plain',
body='do some magic!'
)这为您提供了对其他调整的更多控制。但是,如果简单元组解决方案在您的情况下有效,则不是必需的。
https://stackoverflow.com/questions/64115602
复制相似问题