I have the following FastAPI code:
@app.post('/api_name')
async def api_func(HMAC: str = Header(...)):
try:
return 'success'
except Exception as err:
return err
The issue is that when I do not add HMAC in header, it gives error response
{
"detail": [
{
"loc": [
"header",
"HMAC"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
I want to customise this error response like,
{
"Success": false,
"Error": "HMAC is required in Header"
}
I also tried different code but still it gives same error response
@app.post('/api_name')
async def api_func(HMAC: str = Header(...)):
try:
return 'success'
except HTTPException as err:
if err.status_code == 422 and 'HMAC' in err.detail[0]['loc']:
return {'Success': False, 'Error': 'HMAC is required in Header'}
else:
raise err