目前我在 FastAPI 和文件服务方面遇到了一些困难。
在我的项目中,我有以下工作流程。客户端发送包含从第三方提供商下载文件所需的有效负载的有效负载。
我需要向后端发送有效负载,这是必要的,并且由于创建了资源(下载文件),我假设 POST 将是此端点的方法,但让我向您展示一个示例。
from fastapi import FastAPI, Form
from fastapi.responses import FileResponse
import os
app = FastAPI()
@app.post("/download_file")
async def download():
url = 'https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf'
os.system('wget %s'%url)
return FileResponse("file-sample_150kB.pdf")
@app.get("/get_file")
async def get_file():
return FileResponse("/home/josec/stackoverflow_q/file-sample_150kB.pdf")
Run Code Online (Sandbox Code Playgroud)
如果我访问 http://localhost:8000/get_file,我会在网页上显示文件!但这不是我要找的!我希望通过浏览器或 cli 在客户端下载文件!
以下脚本不会下载任何文件,除非您将其粘贴到可以查看的浏览器中。
import requests
url = "http://localhost:8000/get_file"
response = requests.request("GET", url)
print(response.json())
Run Code Online (Sandbox Code Playgroud)
这个也不好用!
import requests
url = "http://localhost:8000/download_file"
response = requests.request("POST", url)
print(response.json())
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我应该只使用 GET 吗?如果是,我将如何在网址上传递参数?我通过发布请求发送的一些字符串可能会很长,不知道这是否会成为问题。
如何将文件返回给用户?在函数端点的返回语句中立即将其下载给用户!
我可以用 POST 来做吗?
如果你们需要我提供任何其他信息,请告诉我:-)
最好的,
何塞
今天,我给您带来一个看似简单的问题,它并不是看起来那么简单(至少对我而言)!
想象一下,我有以下整数列表:
num = [3,1,1,2]
Run Code Online (Sandbox Code Playgroud)
我想打印与高度相对应的“ $”,即:
num = [3,1,1,2]
Run Code Online (Sandbox Code Playgroud)
&
& &
& & & &
Run Code Online (Sandbox Code Playgroud)
打印此:
for i in num:
print("#"*i)
Run Code Online (Sandbox Code Playgroud)
但是我要显示前者!
我尝试了这个:
& & &
&
&
& &
Run Code Online (Sandbox Code Playgroud)
但是过了一会儿,我了解到这种情况没有任何意义,因为我正在将行号与高度进行比较!
我尝试了其他东西,但没有一个能正常工作,如果有人可以帮助我,我将不胜感激!谢谢