How do I best send Pydantic model objects via put requests?

  Kiến thức lập trình

I am quite new to Pydantic. I have noticed how it is often used in validating input to FastAPI.

I have a project where I need to send something to another application’s API endpoint and thought I would try structuring my data via a model class that inherits from Pydantic’s BaseModel.

I try something like this (MWE):

from uuid import UUID

import requests
from pydantic import BaseModel


class Item(BaseModel):
    id: UUID
    content: str


item = Item(id=UUID(int=1), content="Something")

Now if I try put’ing the object like this:

requests.put("http://localhost", json=item)

it complains with a TypeError saying that “Object of type Item is not JSON serializable”. (It does not matter for the purpose of this demonstration that there is no one listening at ‘localhost’.) OK, this is easy enough to work around:

requests.put("http://localhost", data=item.model_dump_json())

It turns out I need to put a list of Items and then it would be convenient to do something like this, which I cannot due to the error in the first example:

requests.put("http://localhost", json=[item, item])

This becomes somewhat more involved to do via serialising the individual Items.

This makes me wonder: is this the way I should be doing it at all? Probably not, how is it supposed to be done?

Is it the wrong choice in the first place to involve Pydantic here?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT