I am building some small scale application with FastAPI and python. I am trying to add post request to my application but everytime i try to make a post request it shows invalid header with 400 status (bad request).
See the code below- Python + fastApi:
from fastapi import APIRouter, HTTPException, Request, FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
app = FastAPI()
router = APIRouter()
class UserCreate(BaseModel):
user_id: int
username: str
@router.post("/create_user/")
def create_user(user_data: UserCreate):
return {
"msg": "Data received successfully",
"user_id": user_data.user_id,
"username": user_data.username,
}
app.include_router(router, prefix="/mytest")
I am using daphne to as a server to run fastApi + myapplication. something like- daphne -b 127.0.0.1 -p 8000 app.main:app (i tried uvicorn too,getting same error with uvicorn)
Strange- If i run this same code with creating just uvicorn instance something like- Main.py:
from fastapi import FastAPI
from pydantic import BaseModel
app = APIRouter()
class UserCreate(BaseModel):
user_id: int
username: str
@app.post("/create_user/")
async def create_user(user_data: UserCreate):
user_id = user_data.user_id
username = user_data.username
return {
"msg": "we got data succesfully",
"user_id": user_id,
"username": username,
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
The above code executes without any error/exception, python -u main.py
What am i missing?


