How to Post JSON Data to FastAPI Without Swagger UI?
Posting JSON Data to FastAPI Without Swagger UI
When working with FastAPI, it's useful to understand how to post JSON data to its backend without relying on Swagger UI. This approach allows for direct posting of data through a specified URL and retrieval of results in the browser.
Using a Javascript Interface
To achieve this, you can implement a Javascript interface such as Fetch API, which enables the submission of data in JSON format. Consider the following code example:
<code class="javascript">body: JSON.stringify({name: "foo", roll: 1})</code>
This code snippet converts the Javascript object into JSON for transmission.
Frontend Implementation
To interact with the FastAPI backend, you can leverage either of the following methods:
- Jinja2Templates: This technique involves rendering an HTML/JS template that includes a form for submitting data. The form-data can then be converted into JSON.
- Direct JSON Posting: Using Fetch API, you can directly post JSON data without involving a form.
Example Implementation
Consider the following example implementation in Python:
app.py
<code class="python">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})</code>
templates/index.html
<code class="html"><!DOCTYPE html> <html> <body> <h1>Post JSON Data</h1> <form method="post" id="myForm"> name : <input type="text" name="name" value="foo"> roll : <input type="number" name="roll" value="1"> <input type="button" value="Submit" onclick="submitForm()"> </form> <div id="responseArea"></div> <script> function submitForm() { var formElement = document.getElementById('myForm'); var data = new FormData(formElement); fetch('/', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(Object.fromEntries(data)) }) .then(resp => resp.text()) // or, resp.json(), etc. .then(data => { document.getElementById("responseArea").innerHTML = data; }) .catch(error => { console.error(error); }); } </script> </body> </html></code>
By following these steps, you can effectively post JSON data to a FastAPI backend without the need for Swagger UI. This approach allows for greater flexibility and direct interaction with the backend through the specified URL.
The above is the detailed content of How to Post JSON Data to FastAPI Without Swagger UI?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
