From the course: Build REST APIs with FastAPI
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
JSON responses - Python Tutorial
From the course: Build REST APIs with FastAPI
JSON responses
The most natural way to respond with REST APIs is with JSON. And as we saw earlier, if you send back a dictionary, it's going to be converted into a JSON document. But you can do even more with FastAPI and Pydantic. And here we have a time response which has the delta, which is a time delta. And I'm going to calculate the time delta between the start and the end. Both of these are datetime and I have a time response. So the delta is end - start, and I'm returning a time response which is this one where the delta is a delta. So fastapi dev. And we're /Ch04/04_01/server.py. And now I don't need to make you public. Let's go to requests. Right. So I have a request. The start is this, the end is this. And if I'm going to send the request, I'm going to see that it's okay. But I'm getting a very weird reply PT and then one minute 25 seconds. Not sure this is exactly what I want. So one of the great things about Pydantic, it's have a lot of things, including a way to define your own…