Wednesday 2 February 2022

FastAPI: define request to receive form data (x-www-form-urlencoded)

Using Form function, we can specify the form fields.

 

formRequest.py

from fastapi import FastAPI, Form

app = FastAPI()

@app.post("/register-me", status_code=201)
async def login(
    username: str = Form(...), 
    password: str = Form(...),
    country: str = Form(...)):
    return {"username": username, "country": country}

 Open terminal and execute the command ‘uvicorn formRequest:app --reload’.

$uvicorn formRequest:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [83472] using statreload
INFO:     Started server process [83474]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Open the url ‘http://localhost:8000/docs’ in browser and experiment with the api /register-me.

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment