Python 3.x OpenAPI 3 python flask:如何在响应中使用多种内容类型
在OA3文档中,它指出您可以有多种响应内容类型,如下所示:Python 3.x OpenAPI 3 python flask:如何在响应中使用多种内容类型,python-3.x,flask,swagger,openapi,swagger-codegen,Python 3.x,Flask,Swagger,Openapi,Swagger Codegen,在OA3文档中,它指出您可以有多种响应内容类型,如下所示: paths: /users: get: summary: Get all users responses: '200': description: A list of users content: application/json: schema: $ref:
paths:
/users:
get:
summary: Get all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
application/xml:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
text/plain:
schema:
type: string
from io import BytesIO
mem = BytesIO()
mem.write(response_value.encode('utf-8'))
mem.seek(0)
from flask import send_file
return send_file(
mem,
as_attachment=True,
attachment_filename="somefile.csv",
mimetype='text/csv'
)
如何指定要在生成的python flask应用程序的控制器中返回特定的内容类型
以下是我试图实现的规范的响应部分:
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/CoordinateResponse"
text/csv:
schema:
type: string
我的控制器中有一个名为response\u value
的字符串变量,包含CSV文件的内容
我尝试了很多不同的方法,比如
return response_value, 200, {'Content-Type': 'text/csv; charset=utf-8'}
生成内容类型为application/json的响应
及
没有反应
及
没有反应
及
没有反应
如何让它响应
内容类型:text/csv
?最终让它这样工作:
paths:
/users:
get:
summary: Get all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
application/xml:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
text/plain:
schema:
type: string
from io import BytesIO
mem = BytesIO()
mem.write(response_value.encode('utf-8'))
mem.seek(0)
from flask import send_file
return send_file(
mem,
as_attachment=True,
attachment_filename="somefile.csv",
mimetype='text/csv'
)
我一提出问题就无意中找到了答案。我的生活故事。终于让它像这样工作:
paths:
/users:
get:
summary: Get all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
application/xml:
schema:
$ref: '#/components/schemas/ArrayOfUsers'
text/plain:
schema:
type: string
from io import BytesIO
mem = BytesIO()
mem.write(response_value.encode('utf-8'))
mem.seek(0)
from flask import send_file
return send_file(
mem,
as_attachment=True,
attachment_filename="somefile.csv",
mimetype='text/csv'
)
我一提出问题就无意中找到了答案。我的人生故事。这里的
响应值是什么?我在通过get请求返回XML时遇到问题:(@Zafarresponse\u value
只是我的CSV数据。我猜您可以使用mimetype='text/XML'
或mimetype='application/XML'
对XML执行相同的操作。这里的response\u value
是什么?我在通过get请求返回XML时遇到问题:(@Zafarresponse\u value
只是我的CSV数据。我猜您可以使用mimetype='text/XML'
或mimetype='application/XML'