from fastapi import FastAPI, Request
import json
import trio
import hypercorn.trio
from hypercorn.config import Config
app = FastAPI()
@app.post("/test")
async def test(request: Request):
    content_type = request.headers.get("content-type", "")
    if "multipart/form-data" in content_type:
        form = await request.form()
        file_field = form.get("file")
        if hasattr(file_field, "read"):
            body = await file_field.read()
        else:
            body = str(file_field).encode("utf-8")
    else:
        body = await request.body()
    try:
        return {"result": json.loads(body)}
    except Exception as e:
        return {"error": str(e), "body": body.decode('utf-8')}

config = Config()
config.bind = ["127.0.0.1:8000"]
trio.run(hypercorn.trio.serve, app, config)
