从 openapi swagger 上传的文件在后端 python 函数中未收到
我正在尝试使用 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.
我已经引用了此链接: 在 Swagger 中上传文件并在 Flask 后端接收 然而,这是针对 Openapi 2.0 的,并没有帮助,因为我使用的是 openapi 3.0
I am trying to take a file from user using Openapi 3.0 and swagger UI. However i am not getting that file for processing in my python function. Below is my code:
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.
I have already referred this link: Upload a file in Swagger and receive at Flask backend
However this is for Openapi 2.0 and didnt help as I am using openapi 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查是否
可以从请求中获取文件,我不确定该行是否
实际上会获取正确的文件(或仅获取列表?)
check if
can get the file from the request, I'm not sure if the line
will actually get the correct file (or only a list?)
下面的代码更改帮助我解决了这个问题:
api.yaml
在下面的 code.py 中,必须将参数“file”传递给函数,这也是 openapi 规范中的字段名称。 发送文件指针和文件名也很重要
使用 post 请求code.py
Below code changes helped me to get this resolved:
api.yaml
In below code.py it was essential to pass the parameter 'file' to the function which is also the field name in openapi specification. Also its important to send file pointer and filename with the post request
code.py