In the previous article, I built a REST API using Flask, with the following endpoints :
- Get all the movies from a list of movies
- Add a new movie
- Get a single movie from the list
- Get all the actors from a movie
- Add an actor to a movie in the list
You can check out the details here.
Now we will test these endpoints using Postman, which is an open-source software tool for testing API endpoints.
To get started, we need to install the Postman app. Once we have created an account and logged in, we should see this page :
In Postman you can save and group your API requests under collections to reuse them. But for our purpose, we will use Postman to test our APIs only for now. Click on the plus sign next to tests and a window like this should open:
We are now ready to test our API endpoints. Make sure the Flask API we built in the last post is running and we can get started. First, we will test all the GET endpoints. In the window shown above, next to where it shows ‘GET’, enter the URL (http://127.0.0.1:5000/movies) of the first GET endpoint, for fetching all the movies and click on the blue ‘Send’ button.
If everything is working, we should see the list of movies as shown below :
Next, we will check the endpoint for getting a single movie from the list. All we need to do is to change the URL, mention the name of the movie we want to fetch and press send :
Next, we get the list of actors from one movie
Now we will test our POST endpoints. Since we are trying to update or add information using POST, we need to pass this information into the body of our request. Doing this is trivial using Postman. Let's start by testing our first endpoint for POST, which will add a new movie to the movies list.
Click on the dropdown arrow next to GET and select POST. Enter the endpoint (http://127.0.0.1:5000/movies) for adding new movies. Click on the ‘Headers’ tab under the URL. Select ‘content-type under the dropdown for key and ‘application/JSON as the value. Next, click on the ‘Body’ tab and select ‘raw’. Now in the window below, enter the information for the movie you want to add in the JSON format. Press send and you should have the movie details sent back in the body tab below as we had defined in our route
To test our last endpoint, to add actors in a movie, change the URL to http://127.0.0.1:5000/movies/Dune/actors, click on headers and enter key-value pair as above and click on body and as enter information as shown, click send :
You should see the new actor information added along with the other actors in the movie.
Thanks for reading. Your comments and suggestions are welcome.