Skip to content

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".

Postman Create Workspace
Postman Create 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.

Postman Create Request
Postman Create Request

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.

Postman Add Todo URL
Postman Add Todo URL

Now lets send our Request by clicking "Send" button in the top-right corner and see what happens.

Postman Send Request
Postman Send Request

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.

Postman Body Params
Postman Body Params

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.

Postman Body Value
Postman Body Value

Lets try to send our request now.

Postman Add Todo Success
Postman Add Todo Success

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.

MongoDB Atlas New Todo
MongoDB Atlas New Todo

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.

Postman Save Request
Postman Save Request

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.

Postman Save Request Modal
Postman Save Request Modal


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.