0

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")

Postman request: enter image description here

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?

1
  • I am using cors with fastapi Commented Aug 16, 2024 at 8:40

1 Answer 1

0

you have defined your route and included router inside you model, that is causing the issue, if you move them outside it should work fine. I doubt how did that code even run in the first place :D

main.py - uvicorn

from fastapi import APIRouter, FastAPI
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")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)
    

evidence enter image description here

main.py - daphne

from fastapi import APIRouter, FastAPI
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")

# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="127.0.0.1", port=8000)

run with:

daphne -b 127.0.0.1 -p 8000 main:app

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

No that wont work. Code is fine. You can also try it on your machine.
should i paste my output ?? just a sec
Yes, Please!...
let me change to daphne as well
So this working fine. Post-request and you received reply
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.