Today, its in-depth learning for API concept like working, uses and importants API. And now i can write an code for API in Python using FastAPI likely:
installing the fastAPI
"pip install fastapi uvicorn"
create a file called "main.py"
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Welcome to my first API"}
@app.get("/student")
def get_student():
return {
"id": 1,
"name": "Kavin",
"age": 21,
"department": "Computer Science"
}
to run this code, go to the folder containing "main.py" and similar to "Uvicorn running on http://" running , opening through browser and returns the message as "Welcome to my first API "
output:
{
"id": 1,
"name": "Kavin",
"age": 21,
"department": "Computer Science"
}
let's know what happened behind the code
Browser
|
| GET /student
↓
FastAPI
|
↓
get_student()
|
↓
Student data
|
↓
JSON Response
|
↓
Browser
At the end of this I could understand how could API can connect though the server.
Strengthening Learning Skills
Top comments (0)