Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Why does my Python FastAPI server keep returning the status code of 422 when I test it using Pytest?

$
0
0

Here is my FastAPI code. For simplicity purposes, I have made the code to simply return the data passed to it:

import firebase_adminfrom firebase_admin import credentialsfrom firebase_admin import dbfrom firebase_admin import storagefrom fastapi import FastAPI, UploadFile, Filefrom pydantic import BaseModelfrom typing import Optional, Listcred_obj = credentials.Certificate("serviceAccountKey.json")firebase_app = firebase_admin.initialize_app(cred_obj, {'databaseURL': '...' # Cannot reveal})class Item(BaseModel):    req: str    username: Optional[str] = None    project_name: Optional[str] = None    task_name: Optional[str] = None    task_desc: Optional[str] = None    task_status: Optional[str] = None    task_priority: Optional[str] = None    task_due_date: Optional[str] = None    task_assignee: Optional[List[str]] = None    task_assigner: Optional[str] = None    task_attachment: Optional[str] = Noneapp = FastAPI()@app.post("/items/")async def handlereq(item: Item, file: Optional[List[UploadFile]] = File(None)):    return item

FastAPI testclient is used to test the code in a separate code _test.py:

from fastapi.testclient import TestClientfrom main import appclient = TestClient(app)def test_handlereq():    response = client.post("/items/", json={"req": "login", "username": "XXXX", "password": "XXXX"})    assert response.status_code == 200    assert response.json() == {"req": "login", "username": "XXXX", "password": "XXXX"}

I ran the server and the test in split terminals:The server can successfully run but the terminal /item/ returns status code 422

How can I fix the code so that it returns status code 200 and the json is able to be processed?

I tried python request before to post request to the server, but it returned that my data was an unprocessable entity.


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>