Код: Выделить всё
import uvicorn
from fastapi import FastAPI, File, Form
app = FastAPI()
@app.post('/test')
def test(test_item: str = Form(...), test_file: bytes = File(...)):
return {
"test_item": test_item,
"test_file_len": len(test_file),
"test_file_contents": test_file.decode('utf-8')
}
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)
Код: Выделить всё
curl localhost:8000/test -X POST -F test_file=@"test_file.txt" -F test_item="test"
Код: Выделить всё
{
"detail": [
{
"loc": [
"body",
"test_file"
],
"msg": "byte type expected",
"type": "type_error.bytes"
}
]
}
Код: Выделить всё
import uvicorn
from fastapi import FastAPI, File, Form
app = FastAPI()
@app.post('/test')
def test(test_file: bytes = File(...), test_item: str = Form(...)):
return {
"test_item": test_item,
"test_file_len": len(test_file),
"test_file_contents": test_file.decode('utf-8')
}
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)
Подробнее здесь: https://stackoverflow.com/questions/613 ... parameters