POST operations
1. POST operations
Welcome back! We just built our first FastAPI endpoints to handle GET operations. Now, we will learn how to build endpoints to handle POST operations.2. GET vs. POST Operations
Let's start by talking about the main differences between GET and POST operations. First, the traditional use of a GET operation is to request information about an object, while a POST operation is to create a new object. Second, GET request parameters should only be sent via the URL query string, while POST request parameters can also be sent via the request body, which we will cover next. The important thing to remember for now is that POST requests can send much more information to the server than GET requests can. Finally, GET requests can be typed into the URL bar of a web browser, but POST requests require an application or framework like cURL or Python's requests module to be sent.3. HTTP Request Body
Both HTTP requests and responses can include a message body, which is the data sent after the HTTP header. The header specifies the body encoding, which tells the application how to decode this data correctly, meaning that request bodies support nested data structures. JSON and XML are the most common encodings for APIs. We will use JSON since this is FastAPI's default encoding. Here is an example of a request body for creating a movie review. The body can include various data types like strings, objects, numbers, and booleans that are supported by JSON.4. Using pydantic's BaseModel
Since HTTP request bodies support nested data structures, we need more than just type hints to define a message body for a POST request. Python's pydantic library is designed to generate and manage nested model schemas. Pydantic model schemas consist of a named model with named and typed attributes, which inherit from the pydantic BaseModel class. Using pydantic, we can define a model schema for the movie review record described on the last slide. A Review has an integer num_stars, free text, and a boolean for public status. Since Pydantic models follow standard type hint practices, we can specify =False to make the last attribute optional and specify the default is False. MovieReview consists of a movie string and a reference to a Review. Notice that we are nesting Review inside MovieReview to get the nested body structure of the movie reviews shown on the previous slide. This helps us keep our models well-organized. With this structure, we can add attributes to the Review without changing the MovieReview model, and vice-versa.5. Handling a POST Operation
Now let's make a POST endpoint to create a new movie review. The endpoint is called "reviews." We use the @app.post annotation to tell FastAPI that this is a POST operation. We follow it with a function create_review that defines the input and output. The input is the pydantic model for MovieReview objects that we defined in the previous slide. The output schema is called DbReview. Typically we would define a file call crud.py with custom functions to create, read, update, and delete objects in the database. You can see an example of this in the FastAPI docs below. We can then import and use these within our `create_review` function as we are here.6. Let's practice!
Now, let's practice handling POST operations in FastAPI!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.