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.

Solution: Info handler

Solution: Info handler

Let's take a look at my solution. Right. So for getting the time, I'm using the datetime package, and I'm importing datetime and UTC. For getting environment variable, I'm importing environ from the OS module. And then from FastAPI, import fastapi creating the application. Here I'm telling FastAPI that should be a GET request to /info. And then I'm returning a dictionary. The version, 0.1.0. The time is datetime. now, and I'm returning it in UTC. And the user is the one from the environment, USER. Now fastapi and then run and then chapter. And this is the solution server. Okay. So this is running. And now let's open another one and shut down this one, and do a curl http://localhost:8000/info. And we see that we get the version, we get the time, and we see that this is time in UTC. So there's no offset. And the user is VS Code which is the default user on Codespaces. On your machine, it's going to be different.

Contents