Python: FastAPI 422 Unprocessable Entity in use of Union[st,



我试图使用浏览器POST一些数据到API方法(通过在wordpress中使用woocommerce)。当我使用POST请求与模型实体作为一个null值model = None发送数据效果很好,但是当使用一个联盟的东西(st)和没有模型model: Union[st, None] = None"422 Unprocessable Entity"错误。

<处理程序/strong>:

@router.post("", status_code=201)
async def post_webhook(
request: Request,
db: Database = Depends(deps.get_db_async),
settings: MountOlympusSettings = Depends(deps.get_settings),
model: Union[WebhookModel, None] = None,
) -> Any:

,WebhookModel是:

class Billing(BaseModel):
first_name: str
last_name: str
company: str
address_1: str
address_2: str
city: str
postcode: str
country: str
state: str
email: str
phone: str

class Shipping(BaseModel):
first_name: str
last_name: str
company: str
address_1: str
address_2: str
city: str
postcode: str
country: str
state: str
phone: str

class MetaDatum(BaseModel):
id: int
key: str
value: str

class WebhookModel(BaseModel):
id: int
date_created: str
date_created_gmt: str
date_modified: str
date_modified_gmt: str
email: str
first_name: str
last_name: str
role: str
username: str
billing: Billing
shipping: Shipping
is_paying_customer: bool
avatar_url: str
meta_data: List[MetaDatum]
_links: _Links

在此代码中,当模型作为WebhookModel时它是可以的,但是当模型为none时,它不工作并返回422 Unprocessable Entity错误。

可能这个错误是由于以下的打字错误Union[st, None] = None而不是Union[str, None] = None。欢呼声

最新更新