I have the following code that uses Pydantic BaseModel data class
from enum import Enum
import requests
from pydantic import BaseModel
from requests import Response
class PetType(Enum):
DOG: str = 'dog'
CAT: str = 'cat'
class Pet(BaseModel):
name: str
type: PetType
my_dog: Pet = Pet(name='Lucky', type=PetType.DOG)
# This works
resp: Response = requests.post('https://postman-echo.com/post', json=my_dog.json())
print(resp.json())
#This doesn't work
resp: Response = requests.post('https://postman-echo.com/post', json=my_dog.dict())
print(resp.json())
That when I send json equals to model's dict(), I get the error:
> TypeError: Object of type 'PetType' is not JSON serializable
How do I overcome this error and make PetType also serializable?
P.S. The above example is short and simple, but I hit a use case where both cases of sending
json=my_dog.json()
and
json=my_dog.dict()
don't work. This is why I need to solve sending using dict()