1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
Pydantic models are a great way to validate and serialize data for requests and responses.
Pydantic is instrumental in many web frameworks and libraries, such as FastAPI, Django, Flask, and HTTPX.
## `httpx` requests
[`httpx`](https://www.python-httpx.org/) is an HTTP client for Python 3 with synchronous and asynchronous APIs.
In the below example, we query the [JSONPlaceholder API](https://jsonplaceholder.typicode.com/) to get a user's data and validate it with a Pydantic model.
```python {test="skip"}
import httpx
from pydantic import BaseModel, EmailStr
class User(BaseModel):
id: int
name: str
email: EmailStr
url = 'https://jsonplaceholder.typicode.com/users/1'
response = httpx.get(url)
response.raise_for_status()
user = User.model_validate(response.json())
print(repr(user))
#> User(id=1, name='Leanne Graham', email='Sincere@april.biz')
```
The [`TypeAdapter`][pydantic.type_adapter.TypeAdapter] tool from Pydantic often comes in quite
handy when working with HTTP requests. Consider a similar example where we are validating a list of users:
```python {test="skip"}
from pprint import pprint
import httpx
from pydantic import BaseModel, EmailStr, TypeAdapter
class User(BaseModel):
id: int
name: str
email: EmailStr
url = 'https://jsonplaceholder.typicode.com/users/' # (1)!
response = httpx.get(url)
response.raise_for_status()
users_list_adapter = TypeAdapter(list[User])
users = users_list_adapter.validate_python(response.json())
pprint([u.name for u in users])
"""
['Leanne Graham',
'Ervin Howell',
'Clementine Bauch',
'Patricia Lebsack',
'Chelsey Dietrich',
'Mrs. Dennis Schulist',
'Kurtis Weissnat',
'Nicholas Runolfsdottir V',
'Glenna Reichert',
'Clementina DuBuque']
"""
```
1. Note, we're querying the `/users/` endpoint here to get a list of users.
<!-- TODO: httpx, flask, Django rest framework, FastAPI -->
|