1. GET operations
So far, we have learned how the FastAPI framework is used to build web applications that manage data. In this lesson, we will learn how to use FastAPI to handle HTTP GET requests that accept data input.
2. GET operation review
Let's review the GET operation. GET is the most common HTTP operation. It is designed to retrieve information rather than send, update, or delete it. When we type a URL into the address bar of our web browser and hit enter, we are sending a GET request.
The first key component of the request is the host, in this case, "google.com." This identifies the specific server or load balancer on the Internet where we will send the request.
The second key component is the port, in this case 80 which is the default for web serving.
The third key component is the path, in this case, "/search." This tells the server what request handler it should use. This is a specific concern of FastAPI, so we will get back to this soon!
The fourth key component is the query string. In this case, that is "?q=fastapi." This tells the handler that we are sending a query parameter named "q" with a value of "fastapi."
All this put together tells our browser to search Google for the term "fastapi" which will show us the FastAPI project documentation.
3. FastAPI GET operation
Handling a GET request with FastAPI is quite easy! Let's start with the simplest possible FastAPI application.
We import the FastAPI module at the top with "from fastapi import FastAPI." Then, we instantiate our app on the next line as an instance of the class FastAPI.
The last part tells the app to handle GET requests to root, which is either the host alone or the host followed by a single slash. We do this with the annotation "@app.get("/")". Then, we provide a function called "root()" that returns a response.
The application responds to requests to root by sending back a static dictionary with the key "message" and the value "Hello World." When it is returned, FastAPI encodes this dictionary as JSON.
4. Using the cURL web client
cURL is a convenient script we can use to test our code without a browser. cURL stands for "client URL."
The only required argument to cURL is the URL. Some key optional arguments are "verbose" to make the client more talkative, "header" to specify the encoding of POST data, and "data" for the data itself.
If we call cURL on the endpoint from the previous slide, it prints the response, a message of "Hello world."
5. Query Parameters
Now, let's make a GET endpoint that supports query parameters. We can add a new path to our application called "hello" that takes an optional parameter called "name" with the default value "Alan." We add the optional query parameter to the function "hello" just like we would add a keyword parameter to any Python function using type hints.
This endpoint will return a message "Hello Alan" unless there is a name provided in the query string, in which case it will substitute the name parameter.
In this case, we provided the name "Steve," which is shown on the lower screenshot.
6. Let's practice!
Now, let's practice serving GET operations with FastAPI!