0

I'm trying to POST some data to an API method using the browser (By using woocommerce in wordpress). When I use POST request with model entity as a null value model = None to send data it works fine, but when use an Union of something (st) and None as model model: Union[st, None] = None "422 Unprocessable Entity" error.

Handler:

@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:

That WebhookModel is:

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

In this code, when model is as a WebhookModel it's ok, but when model is none, it dos't work and return 422 Unprocessable Entity Error.

  • 1
    You need to add more information, like the structure of the model used and the tests you're using, otherwise it will be impossible for anybody to reproduce the situation and understand where the problem could lie – lsabi Jul 16 '22 at 13:20
  • 1
    As Isabi wrote, we need more info, a reproducible example, including how you actually run the code. When I run a basic test with a POST endpoint with only argument `model: Union[WebhookModel, None] = None`, it works beautifully to call it without a body (`curl -X 'POST' 'http://localhost:8000/' -H 'Content-Type: application/json'`). – M.O. Jul 17 '22 at 19:05
  • I added more details about my question. – Ali SHOKOUH ABDI Jul 19 '22 at 11:08

1 Answers1

0

Maybe the error is due to the following typo Union[st, None] = None instead of Union[str, None] = None. Cheers

DeFeNdog
  • 1,156
  • 1
  • 12
  • 25