从 openapi swagger 上传的文件未在后端 python 函数中收到
file uploaded from openapi swagger not getting received at backend python function
我正在尝试使用 Openapi 3.0 和 swagger UI 从用户那里获取文件。但是我没有在我的 python 函数中获取该文件进行处理。下面是我的代码:
code.py
def get_file():
try:
file=request.files.getlist('file')[0]
with open(file, 'r') as fp:
files = {"file": (file, fp)}
response = requests.post(server, files=files)
return response.json()
except Exception as exc:
return exc
api.yaml
/get-result:
post:
summary: "A function to get file"
operationId: "code.get_file"
requestBody:
content:
application/json:
schema:
type: string
format: binary
responses:
200:
description: "executed successfully"
content:
application/json:
schema:
$ref: "#/components/schemas/myschema"
500:
description: Server is down.
我已经提到了这个link:Upload a file in Swagger and receive at Flask backend
然而,这是针对 Openapi 2.0 的,并没有帮助,因为我使用的是 openapi 3.0
检查是否
request.files['file']
可以从请求中获取文件,我不确定行
file=request.files.getlist('file')[0]
实际上会得到正确的文件(或只是一个列表?)
下面的代码更改帮助我解决了这个问题:
api.yaml
/get-result:
post:
summary: "A function to get file"
operationId: "code.get_file"
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
format: binary
responses:
200:
description: "executed successfully"
content:
application/json:
schema:
$ref: "#/components/schemas/myschema"
500:
description: Server is down.
下面code.py必须将参数'file'传递给函数,这也是openapi规范中的字段名称。使用 post 请求
发送文件指针和文件名也很重要
code.py
def get_file(file):
try:
fp=file.read()
file.save(file.filename)
response = requests.post(server, files={"file":(file.filename,fp)})
return response.json()
except Exception as exc:
return exc
我正在尝试使用 Openapi 3.0 和 swagger UI 从用户那里获取文件。但是我没有在我的 python 函数中获取该文件进行处理。下面是我的代码:
code.py
def get_file():
try:
file=request.files.getlist('file')[0]
with open(file, 'r') as fp:
files = {"file": (file, fp)}
response = requests.post(server, files=files)
return response.json()
except Exception as exc:
return exc
api.yaml
/get-result:
post:
summary: "A function to get file"
operationId: "code.get_file"
requestBody:
content:
application/json:
schema:
type: string
format: binary
responses:
200:
description: "executed successfully"
content:
application/json:
schema:
$ref: "#/components/schemas/myschema"
500:
description: Server is down.
我已经提到了这个link:Upload a file in Swagger and receive at Flask backend 然而,这是针对 Openapi 2.0 的,并没有帮助,因为我使用的是 openapi 3.0
检查是否
request.files['file']
可以从请求中获取文件,我不确定行
file=request.files.getlist('file')[0]
实际上会得到正确的文件(或只是一个列表?)
下面的代码更改帮助我解决了这个问题:
api.yaml
/get-result:
post:
summary: "A function to get file"
operationId: "code.get_file"
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
format: binary
responses:
200:
description: "executed successfully"
content:
application/json:
schema:
$ref: "#/components/schemas/myschema"
500:
description: Server is down.
下面code.py必须将参数'file'传递给函数,这也是openapi规范中的字段名称。使用 post 请求
发送文件指针和文件名也很重要code.py
def get_file(file):
try:
fp=file.read()
file.save(file.filename)
response = requests.post(server, files={"file":(file.filename,fp)})
return response.json()
except Exception as exc:
return exc