FastAPI POST Requests
00:12 And this is an acronym that stands for MongoDB, Express, Angular, and Node. There’s also the MERN stack that replaces Angular with React. If you add React and MongoDB to FastAPI, you get the FARM stack.
00:29 This course won’t be covering React, and it won’t go that in-depth into MongoDB, but here’s a crash course on how to quickly get started with MongoDB. So Mongo DB is a NoSQL database. TLDR, NoSQL means non-relational.
00:47 So instead of tables created by keys, MongoDB persists data in something called a document. The structure of a MongoDB database is a group of collections, and each collection is a group of documents.
01:11 That’s oversimplifying a real-world implementation a bit, but it is enough to get you started in this course. A real-world MongoDB database, just like a real-world relational database, lives on a server, and you have to connect to it via a network.
01:26 This can get tricky and be a hassle while developing. Thus for relational databases, many developers rely upon SQLite. This is a file-based database that doesn’t require the ceremony of connecting to a server.
Creating a Mongita database requires just a single line of code. You can create one on disk or in memory. This course will use the on-disk client. Import the
MongitaClientDisk class from the
mongita module in
To insert a new document, call the
.insert_one() method with a dictionary. And to see the inserted document, get all the documents in the
shapes collection with the
.find() method on the collection. The dictionary here is a filter.
03:08 An empty dictionary means retrieve all documents in the collection. So how does this relate to FastAPI? Let’s take a look. You want to be able to insert a new shape into the database from the API, and that’s going to involve a POST request.
Just like the
@get decorator you’ve seen up to this point, there is also a
@post decorator. With a REST API, the data to create a new item is often included in the request body as JSON. As you’ve seen, the shape has a string name, integer number of sides, and finally an integer ID. To communicate this to FastAPI, you’ll use a pydantic base model.
And finally, it is customary to return the newly created item in the body of a response from a POST request. Next, change the
.get_shapes() method to remove the
shape_id and return all of the documents in the
This code creates a dictionary for every document in the
shapes collection. Since the
object_id with key
_id that Mongita assigns to the document cannot automatically be serialized to JSON, add an
if clause to remove it. Real quick, before looking at Swagger, update the
get_shape_by_id() function to work with Mongita. The decorator and signature will remain the same.
and omit the
_id key as it can’t be implicitly converted the JSON. And raise an exception if the document is not found. Time to look at Swagger. After the application restarts, expand the POST request for the
/shapes endpoint and click the Try it out button. The Request body is a text area with a boilerplate JSON document. Fill it out with the details of a new shape.
06:56 This command formats a JSON string from the key-value pairs in the body of the POST request. As mentioned earlier, the body of the POST request for a REST API has the JSON data for the item to create.
And you can see that a status of
200 is returned, along with the JSON for the new item in the body. So FastAPI serializes and deserializes the
BaseModel subclasses to and from JSON for you, and also enforces the type hints for the model properties.
Become a Member to join the conversation.