Skip to content

FastAPI backend login issue status code 422 #4

Description

@scheung38

Tried to swap out node.js for FastAPI as there are some additional business logic thats currently only in Python:

server-fastapi/routers/auth.py

import logging
from fastapi import APIRouter, Request
from pydantic import BaseModel
from typing import Union
import requests
    
router = APIRouter(prefix="/auth", 
                   tags=["auth"], 
                   responses={404: {"description": "Not found"}}
                   )

class User(BaseModel):
    username: str
    secret: str
    email: Union[str, None] = None
    first_name: Union[str, None] = None
    last_name: Union[str, None] = None
    
@router.post("/login")
async def login(user: User):
    payload={}
    response = requests.get('https://api.chatengine.io/users/me', 
        headers={ 
            "Content-Type": "application/json",    
            "Project-ID": <MY_PROJECT_ID>,
            "User-Name": user.username,  
            "User-Secret": user.secret  
        },
        data=payload
    )
    logging.info("response: {}".format(response))
    logging.info("type(response): {}".format(type(response)))
    
    return response.json()
 
@router.post("/signup")
async def signup(user: User):
    response = requests.post('https://api.chatengine.io/users/', 
        data={
            "username": user.username,
            "secret": user.secret,
            "email": user.email,
            "first_name": user.first_name,
            "last_name": user.last_name,
        },
        headers={ "Private-Key": PRIVATE_KEY }
    )
    return {"response": response.json()}

server-fastapi/main.py

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# pip install "uvicorn[standard]"

from pydantic import BaseModel
from typing import Union

import requests
from routers import auth, openai

app = FastAPI()

origins = [
    "http://localhost:5173", 
]
 
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth.router)
app.include_router(openai.router)

cd server-fastapi
uvicorn main:app --reload --port 1337

Status code 422:

But

FastAPI Swagger /auth/login works

as well as standalone test_login.py as well as in Isomnia

import requests

url = "https://api.chatengine.io/users/me/"

payload={}
headers = {
  'Project-ID': '<MY_PROJECT>',
  'User-Name': 'testUser',
  'User-Secret': 'testUser'
}

response = requests.request("GET", url, headers=headers, data=payload)


print(response.json())

So not sure why 422 when trying to authenticate with FastAPI fails:

Failed to load resource: the server responded with a status of 422 (Unprocessable Entity) and No resource with given URL found.

Is this related to how payload must be altered in the client/src/state/api.js payload?

Screenshot 2024-01-25 at 00 09 12

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions