如何multipart/form-data在python中发送请求?如何发送文件,我理解,但如何通过这种方法发送表单数据无法理解.
我正在构建一个简单的 API 来测试数据库。当我使用 get request 时一切正常,但如果我更改为 post,我会收到“无法处理的实体”错误:
这是 FastAPI 代码:
from fastapi import FastAPI
app = FastAPI()
@app.post("/")
def main(user):
return user
Run Code Online (Sandbox Code Playgroud)
然后,我的请求使用 javascript
let axios = require('axios')
data = {
user: 'smith'
}
axios.post('http://localhost:8000', data)
.then(response => (console.log(response.url)))
Run Code Online (Sandbox Code Playgroud)
并使用 Python
import requests
url = 'http://127.0.0.1:8000'
data = {'user': 'Smith'}
response = requests.post(url, json=data)
print(response.text)
Run Code Online (Sandbox Code Playgroud)
我也尝试解析为 json,使用 utf-8 编码,并更改标题。没有什么对我有用。
我正在尝试将一个大文件 (\xe2\x89\xa53GB) 上传到我的 FastAPI 服务器,而不将整个文件加载到内存中,因为我的服务器只有 2GB 可用内存。
\n服务器端:
\nasync def uploadfiles(upload_file: UploadFile = File(...):\nRun Code Online (Sandbox Code Playgroud)\n客户端:
\nm = MultipartEncoder(fields = {"upload_file":open(file_name,\'rb\')})\nprefix = "http://xxx:5000"\nurl = "{}/v1/uploadfiles".format(prefix)\ntry:\n req = requests.post(\n url,\n data=m,\n verify=False,\n )\nRun Code Online (Sandbox Code Playgroud)\n返回:
\nasync def uploadfiles(upload_file: UploadFile = File(...):\nRun Code Online (Sandbox Code Playgroud)\n我不确定MultipartEncoder实际发送到服务器的内容,因此请求不匹配。有任何想法吗?
python file-upload starlette fastapi python-requests-toolbelt
我有这个模型:
class Text(BaseModel):
id: str
text: str = None
class TextsRequest(BaseModel):
data: list[Text]
n_processes: Union[int, None]
Run Code Online (Sandbox Code Playgroud)
所以我希望能够接受如下请求:
{"data": ["id": "1", "text": "The text 1"], "n_processes": 8}
Run Code Online (Sandbox Code Playgroud)
和
{"data": ["id": "1", "text": "The text 1"]}.
Run Code Online (Sandbox Code Playgroud)
现在在第二种情况下我得到
{'data': [{'id': '1', 'text': 'The text 1'}], 'n_processes': None}
Run Code Online (Sandbox Code Playgroud)
使用此代码:
app = FastAPI()
@app.post("/make_post/", response_model_exclude_none=True)
async def create_graph(request: TextsRequest):
input_data = jsonable_encoder(request)
Run Code Online (Sandbox Code Playgroud)
n_processes那么这里我该如何排除呢?
我根据官方文档使用fastapi上传文件,就像:
@app.post("/create_file/")
async def create_file(file: UploadFile=File(...)):
file2store = await file.read()
# some code to store the BytesIO(file2store) to the other database
Run Code Online (Sandbox Code Playgroud)
当我使用 python requests lib 发送请求时:
f = open(".../file.txt", 'rb')
files = {"file": (f.name, f, "multipart/form-data")}
requests.post(url="SERVER_URL/create_file", files=files)
Run Code Online (Sandbox Code Playgroud)
file2store 始终为空。有时(很少见),它可以获取文件字节,但几乎所有时间都是空的,所以我无法在另一个数据库上恢复文件。我还尝试了 'bytes' 而不是 'UploadFile',我得到了相同的结果。我的代码有什么地方不对,还是我使用fastapi上传文件的方式有问题?我google了很长时间,但没有成功。所以我在这里提出问题,希望知道答案的人可以帮助我。谢谢
我有一个 FastAPI 应用程序,我希望将默认日志写入 STDOUT,并使用 JSON 格式的以下数据:
应用程序日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "app",
"timestamp": "2022-01-16T08:30:08.181Z",
"file": "api/predictor/predict.py",
"line": 34,
"threadId": 435454,
"message": "API Server started on port 8080 (development)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
访问日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "access",
"timestamp": "2022-01-16T08:30:08.181Z",
"message": "GET /app/health 200 6ms"
},
"req": {
"url": "/app/health",
"headers": {
"host": "localhost:8080",
"user-agent": "curl/7.68.0",
"accept": "*/*"
},
"method": "GET",
"httpVersion": "1.1",
"originalUrl": "/app/health",
"query": {}
},
"res": {
"statusCode": 200,
"body": …Run Code Online (Sandbox Code Playgroud) 我创建了一个端点,如下所示:
@app.post("/report/upload")
def create_upload_files(files: UploadFile = File(...)):
try:
with open(files.filename,'wb+') as wf:
wf.write(file.file.read())
wf.close()
except Exception as e:
return {"error": e.__str__()}
Run Code Online (Sandbox Code Playgroud)
它是用 uvicorn 启动的:
../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)
我正在执行一些测试,使用 Python 请求上传大约100 MB的文件,大约需要 128 秒:
../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)
我使用 Flask 通过 API 端点测试了相同的上传脚本,大约需要 0.5 秒:
f = open(sys.argv[1],"rb").read()
hex_convert = binascii.hexlify(f)
items = {"files": hex_convert.decode()}
start = time.time()
r = requests.post("http://192.168.0.90:5000/report/upload",files=items)
end = time.time() - start
print(end)
Run Code Online (Sandbox Code Playgroud)
我做错了什么吗?
我一直在使用FastAPI来创建基于 HTTP 的 API。它目前支持 JSON 编码的参数,但我也想支持form-urlencoded(甚至理想情况下form-data)同一 URL 上的参数。
按照尼基塔的回答,我可以获得单独的网址:
from typing import Optional
from fastapi import FastAPI, Body, Form, Depends
from pydantic import BaseModel
class MyItem(BaseModel):
id: Optional[int] = None
txt: str
@classmethod
def as_form(cls, id: Optional[int] = Form(None), txt: str = Form(...)) -> 'MyItem':
return cls(id=id, txt=txt)
app = FastAPI()
@app.post("/form")
async def form_endpoint(item: MyItem = Depends(MyItem.as_form)):
print("got item =", repr(item))
return "ok"
@app.post("/json")
async def json_endpoint(item: MyItem = Body(...)):
print("got …Run Code Online (Sandbox Code Playgroud) 使用 ORM,我想要执行一个 POST 请求,让某些字段具有null值,该值将在数据库中转换为那里指定的默认值。
问题是 OpenAPI (Swagger) docs忽略了默认值None,并且默认情况下仍然提示 a UUID。
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
from uuid import UUID
import uvicorn
class Table(BaseModel):
# ID: Optional[UUID] # the docs show a example UUID, ok
ID: Optional[UUID] = None # the docs still shows a uuid, when it should show a null or valid None value.
app = FastAPI()
@app.post("/table/", response_model=Table)
def create_table(table: Table):
# here we call …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 FastAPI 应用程序中编写一个中间件,以便到达与特定格式匹配的端点的请求将被重新路由到不同的 URL,但我无法找到一种方法来做到这一点,因为它request.url是只读的。
我还在寻找一种在重新路由之前更新请求标头的方法。
这些事情在 FastAPI 中可能实现吗?
重定向是迄今为止我能做的最好的事情:
from fastapi import Request
from fastapi.responses import RedirectResponse
@app.middleware("http")
async def redirect_middleware(request: Request, call_next):
if matches_certain_format(request.url.path):
new_url = create_target_url(request.url.path)
return RedirectResponse(url=new_url)
Run Code Online (Sandbox Code Playgroud)