The idea is to get file object from one endpoint and send it to other endpoints to work with it without saving it. Let's have this expample code:
import httpx
from fastapi import Request, UploadFile, File
app = FastAPI()
client = httpx.AsyncClient()
@app.post("/endpoint/")
async def foo(request: Request, file: UploadFile = File(...))
urls = ["/some/other/endpoint", "/another/endpoint/"]
for url in urls:
response = await client.post(url) # here I need to send the file to the other endpoint
return {"bar": "baz"}
@app.post("/some/other/endpoint/")
async def baz(request: Request, file: UploadFile = File(...)): # and here to use it
# Do something with the file object
return {"file": file.filename}
@app.post("/another/endpoint/")
async def baz(request: Request, file: UploadFile = File(...)): # and here to use it too
# Do something with the file object
return {"file": file.content_type}
As stated here I tried to do something like this:
data = {'file': file}
response = await client.post(url, data=data)
But it errored with
'{"detail":[{"loc":["body","file"],"msg":"Expected UploadFile, received: <class \'str\'>","type":"value_error"}]}'
Example curl request:
curl -X 'POST' -F 'file=@somefile' someserver/endpoint/