Testing Add Todo Endpoint
Since our endpoint for adding todos uses POST method we cannot test it by just accessing the link.
You will need to use some tool for REST APIs testing like Postman or some other. For this project we will Postman (https://www.postman.com/ā). You will need to download and install it from their website.
After you download and sign in to Postman successfully, you should create a new āWorkspaceā.
Now we need to create and setup āRequestā in order to be able to test it. We can do it by clicking ā+ā button as on the image below.
After we created our new Request in Postman, lets configue it by adding our enpoint to the url input [1] and set our request type to āPOSTā [2] as shown on the image below.
Now lets send our Request by clicking āSendā button in the top-right corner and see what happens.
If everything went through as expected, you will notice in the bottom of your Postman screen the following response from the backend with status code 400 (Bad Request)
{
"message": "Title value cannot be empty."
}
Why this happens?
Because we protected our endpoint for adding new todo items to decline every request without title defined and we are not sending any body parameters from Postman.
To define our title in body parameters we will click the āBodyā tab [1], set our body type to ārawā [2] and set value to be type of JSON [3] since our enpoint expects JSON object for the request body.
Now, lets add our object that contains title as an attribute to be sent in the request. We will set value to āTodo 1ā for testing purposes.
Lets try to send our request now.
As you can see, we got successfull response with status code 200 which means our new todo was added successfully to the database.
To verify that, we can go to MongoDB Atlas, enter our āCluster0ā, go to āCollectionsā and see if it exists in ātodosā collection.
If you followed all the steps, you should be able to see a new todo object added to ātodosā collection with title āTodo 1ā as on image above.
Last but not least, lets save our Request in Postman so we can use later if needed. We can do it by simply clicking āSaveā button as on the image below.
Modal should popup that will ask you to select the Collection where it should store the Request. We will name our request as āAdd Todo Itemā, select āNew Collectionā and hit the āSaveā button in the bottom-right corner.
We now tested adding our new todo item and it works. Lets proceed and add an endpoint on backend that will allow us to delete the specific todo item.