首頁  >  文章  >  後端開發  >  如何建立接受表單或 JSON 正文的 FastAPI 端點?

如何建立接受表單或 JSON 正文的 FastAPI 端點?

DDD
DDD原創
2024-10-27 06:16:03370瀏覽

How to Create a FastAPI Endpoint That Accepts Either Form or JSON Body?

如何建立可以接受 Form 或 JSON body 的 FastAPI 端點?

在 FastAPI 中,您可以定義處理各種類型請求正文的端點,例如JSON 或表單資料。這允許您建立可以接受任一格式的端點,而無需單獨的端點。

要實現此目的,您可以遵循以下方法之一:

選項1:使用依賴函數

您可以利用依賴函數來驗證請求的Content-Type 標頭,然後使用Starlette 的方法適當地解析正文。請注意,僅依賴 Content-Type 標頭可能無法始終保證請求正文的有效性,因此建議包含錯誤處理。

<code class="python">import os, sys
from fastapi import FastAPI, Depends, HTTPException
from starlette.requests import Request
app = FastAPI()

# Generating file
open("./app.txt", "w").write("hello from a file")

async def body_parser(request: Request):
    ct = request.headers.get("Content-Type", "")
    if ct == "application/json":
        try:
            d = await request.json()
            if not isinstance(d, dict):
                raise HTTPException(status_code=400, details={"error":"request body must be a dict"})
            return d
        except JSONDecodeError:
            raise HTTPException(400, "Could not parse request body as JSON")
    elif ct == "multipart/form-data":
        await request.stream()  # this is required for body parsing.
        d = await request.form()
        if not d:
            raise HTTPException(status_code=400, details={"error":"no form parameters found"})
        return d
    else:
        raise HTTPException(405, "Content-Type must be either JSON or multipart/form-data")

@app.post("/", dependencies=[Depends(body_parser)])
async def body_handler(d: dict):
    if "file" in d:
        return {"file": d["file"]}
    return d</code>

選項 2:利用選用表單/檔案參數

在此方法中,您可以在端點中將表單/檔案參數定義為可選。如果這些參數中的任何一個具有值,則它假定是表單資料請求。否則,它將驗證請求正文為 JSON。

<code class="python">from fastapi import FastAPI, Form, File, UploadFile
app = FastAPI()

@app.post("/")
async def file_or_json(
    files: List[UploadFile] = File(None),
    some_data: str = Form(None)
):
    if files:
        return {"files": len(files)}
    return {"data": some_data}</code>

選項 3:為每種類型定義單獨的端點

您也可以建立單獨的端點,一個用於 JSON,另一個用於表單資料。使用中間件,您可以檢查 Content-Type 標頭並將請求重新路由到適當的端點。

<code class="python">from fastapi import FastAPI, Request, Form, File, UploadFile
from fastapi.responses import JSONResponse
app = FastAPI()

@app.middleware("http")
async def middleware(request: Request, call_next):
    ct = request.headers.get("Content-Type", "")
    if ct == "application/json":
        request.scope["path"] = "/json"
    elif ct in ["multipart/form-data", "application/x-www-form-urlencoded"]:
        request.scope["path"] = "/form"
    return await call_next(request)

@app.post("/json")
async def json_endpoint(json_data: dict):
    pass

@app.post("/form")
async def form_endpoint(file: UploadFile = File(...)):
    pass</code>

選項4:引用另一個答案以獲得替代方法

此外,您可以在Stack Overflow 上找到這個答案很有幫助,因為它提供了在單一端點中處理JSON 和表單資料的不同視角:

https://stackoverflow.com/a/67003163/10811840

測試選項1、2 和3

出於測試目的,您可以使用請求庫:

<code class="python">import requests

url = "http://127.0.0.1:8000"
# for testing Python 3.7 and above use:
# url = "http://localhost:8000"

# form-data request
files = [('files', ('a.txt', open('a.txt', 'rb'), 'text/plain'))]
response = requests.post(url, files=files)
print(response.text)

# JSON request
data = {"some_data": "Hello, world!"}
response = requests.post(url, json=data)
print(response.text)</code>

這些方法提供了不同的方法來建立可以處理JSON 和表單的端點-FastAPI 中的資料。選擇最適合您的要求和用例的方法。

以上是如何建立接受表單或 JSON 正文的 FastAPI 端點?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn