I am writing a script to post data from an internal system to a third party web API. Our internal system uses data Models defined in Pydantic. I am trying to send data in one of these models to the API using requests.post
but it can’t serialize some types and it’s driving me insane.
Take this data model as an example:
class Product(BaseModel):
id: int
group_id: int
sku: str
price: Decimal
special_offer_price: Decimal
last_update: datetime.date
If I try to send data using this Model using requests.post
thus:
product_item = website_api.Product(**product.__dict__)
requests.post(url, headers=jwt_auth.auth_header, json=product_item)
The str
and int
fields are fine but Decimal
and datetime.date
fields throw up an exception as they are not serializable, giving the error somewhat like:
TypeError: Object of type date is not JSON serializable
I have tried adding a @field_serializer
for the fields to the class definition for pydantic;
@field_serializer("price", "special_offer_price")
def serialize_decimal(self, value):
return str(value)
@field_serializer("last_update")
def serialize_date(self, value):
return str(value)
but requests
doesn’t seem to use it when serializing.