Home > other >  FastAPI, SQLalchemy; By using Postman I can't post raw JSON body request correctly. It works fi
FastAPI, SQLalchemy; By using Postman I can't post raw JSON body request correctly. It works fi

Time:11-17

My @router.post is like this:

@router.post("/filter/filtering")
async def filter_test(skip: int = 0, limit: int = 100, company_name: str = None, db: Session = Depends(get_db)):
    _audits = crud.filter_test(db, company_name)
    return Response(status="Ok", code="200", message="Success fetch all data", result=_audits)

From crud.py, filter_test function is:

def filter_test(db: Session, company_name: str = None):
    # if company_name:
    return db.query(Audit).filter(Audit.company_name == company_name).all()

When I post request with params as "company_name" as key and "panda" as value, it gives me correct answers. But when I try to do it in a Body format, fails me.

With params output:


{
    "code": "200",
    "status": "Ok",
    "message": "Success fetch all data",
    "result": [
        {
            "product_name": "dondurma",
            "audit_date": "2022-06-06T00:00:00",
            "request_date": "2022-11-14T18:11:29.450855",
            "correct_company_name": null,
            "company_name": "panda",
            "image_link": "www.test.com",
            "id": 5,
            "correct_name": null,
            "correction_date": null
        },
        {
            "product_name": "dondurma1",
            "audit_date": "2022-06-06T00:00:00",
            "request_date": "2022-11-14T18:40:18.925370",
            "correct_company_name": null,
            "company_name": "panda",
            "image_link": "www.test.com",
            "id": 6,
            "correct_name": null,
            "correction_date": null
        },
        {
            "product_name": "dondurma1",
            "audit_date": "2022-06-06T00:00:00",
            "request_date": "2022-11-14T18:40:18.925370",
            "correct_company_name": null,
            "company_name": "panda",
            "image_link": "www.test.com",
            "id": 7,
            "correct_name": null,
            "correction_date": null
        }
    ]
}

Here is my JSON BODY format:


{
    "parameter":{
        
        "company_name":"panda"


    }
}

This one is the output of the JSON Body format:


{
    "code": "200",
    "status": "Ok",
    "message": "Success fetch all data",
    "result": [
        {
            "product_name": "dondurma1",
            "audit_date": "2022-06-06T00:00:00",
            "request_date": "2022-11-14T18:40:18.925370",
            "correct_company_name": "kratos",
            "company_name": null,
            "image_link": "www.test.com",
            "id": 8,
            "correct_name": "kratos",
            "correction_date": null
        }
    ]
}

What might be the problem is? Thanks in advance

Tried with params, it works fine as for json body format it fails. Screenshot for postman is here: enter image description here

CodePudding user response:

You either need to define a pydantic model as the body, or else use Body which is fine in your case as you're only accepting one value:

from typing import Optional
from fastapi import Body  # not mentioning all the other fastapi imports here for brevity

@router.post("/filter/filtering")
async def filter_test(skip: int = 0, limit: int = 100, company_name: Optional[str] = Body(default=None, embed=True), db: Session = Depends(get_db)):
    _audits = crud.filter_test(db, company_name)
    return {'status': "Ok", 'code': "200", 'message': "Success fetch all data", 'result': _audits}

This will work passing a request body of {} or {"company_name": null} or {"company_name": "foo"} - the first two cases the company_name variable will default to None.

The Response in your example doesn't work for me if it's a fastAPI Response, is that a custom class you have? Here I'm just returning a dictionary which will work fine provided it's json serializable.

You might consider dropping the status and code from that dict as the framework is already returning HTTP status 200 by default (i.e. not part of the body), or you can pass a custom status_code via returning a JSONResponse.

  • Related