これらの手法を使用すると、Swagger UI インターフェイスに依存せずに、JSON データを FastAPI バックエンドに簡単にポストできます。
","image":"http://www.luping.net/uploads/20241025/1729834590671b2e5e700bb.jpg","datePublished":"2024-11-07T00:35:56+08:00","dateModified":"2024-11-07T00:35:56+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}FastAPI を使用する場合、Swagger UI を仲介せずに JSON データを投稿できます。これを実現する方法は次のとおりです。
Fetch API などの JavaScript ベースのインターフェイスを使用して、JSON 形式でデータを送信します。次に例を示します:
var data = {
name: "foo",
roll: 1
}
fetch('/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(resp => {
return resp.text();
}).then(data => {
// Handle the response
});
また、Jinja2 テンプレートと HTML フォームを利用してデータを送信することもできます。方法は次のとおりです:
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
app = FastAPI()
templates = Jinja2Templates(directory="templates")
class Item(BaseModel):
name: str
roll: int
@app.post("/")
async def create_item(item: Item):
return item
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
これらの手法を使用すると、Swagger UI インターフェイスに依存せずに、JSON データを FastAPI バックエンドに簡単にポストできます。
免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3