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.
Writing handlers - Python Tutorial
From the course: Build REST APIs with FastAPI
Writing handlers
Let's see how you write handlers. These are the function that accepts a request and returns a response. So here we have our first code of FastAPI and FastAPI, I say it's fast in two different planes. One is fast as we think about it; it's a very fast server. But also, it's very fast to develop with. And this is all the code you need to write in order to get a single server running. So from FastAPI, we import FastAPI and then we create our application which is FastAPI without any parameters to it. And then we define our handler. Right now our handler is not going to get any data, it's just going to return something. And because FastAPI is built for REST APIs, which means JSON, you just return a dictionary and automatically FastAPI is going to convert it to a JSON object. And to tie it up, you need to use a decorator. This get decorator tells FastAPI that when someone does a GET request to /health, this is the function you need to call. And that's it. That's all the code you need to…