CodeTreat

Full Stack Developer Tutorials

  • About
Testing REST API using Postman

Testing REST API using Postman

We have built a full-fledged REST API to perform CRUD operations in the REST API CRUD tutorial. In this guide, we will look on how to use the API testing tool Postman to test the API we built. Postman is a powerful development environment to test REST APIs. It provides a rich user interface to perform various HTTP operations on our API.

Introduction to Postman


Postman is a full blown API development environment which comes with numerous options to test and monitor the REST APIs. Postman comes with additional tools that can be used to monitor the APIs and also allows collaborative features such as allowing multiple users to publish and test the APIs. The documentation feature enables the developers to build and publish web-viewable documentation.

Install Postman


  • Postman offers binaries for all operating system platforms such as Mac, Windows and Linux.
  • Visit https://www.getpostman.com/apps and select the appropriate operation system platform that you use and click Download.
  • After downloading the setup file, proceed with the installation which is straight forward.
  • The setup will create shortcuts that will allow us to open the application.

After successful completion of the installation, open the application by running the shortcut from the desktop. You should be able to see a window similar to the one shown below.

Postman startup

Testing the API


Before testing our API, let us ensure that the application is up and running by starting the Spring Boot CRUD app which we built. Right click on the project and select the “Run As -> Spring Boot App” as shown below. Alternatively you can click the green color play button from the toolbar which is highlighted in the image show below, to start the application.

Launching Spring Boot Applicatio

Check the console and confirm that the application has been started successfully without any errors. Let us test all operations on our API such as Create, Read, Update and Delete through Postman.

Create

  • To create a new record in the DB through the API, enter the URI localhost:8080/createsample in the URI field and select the HTTP method as POST from the drop down menu displayed before the URI field as shown below

Postman GET request

  • Since we are creating a new record in the DB, we have to mention the details of the record to be created. In this sample, we have to pass the values name and age as parameters to the URI.
  • In-order to pass the parameters, select the correct options and enter the following details in the appropriate fields as shown in the below image.

Postman GET request success

  1. Click the Body option from the menu before the URI field.
  2. In the menu displayed below that, select the raw option.
  3. Select the JSON (application/json) from the drop down menu displayed at the end of the options list.
  4. In the body section, enter the JSON data {“name”:”Ron”, “age”:”27″}.
  5. Finally, click the Send button displayed near the URI field to initiate the request.
  6. The request would take few moments and the success result will be displayed in the response section.
  7. The request status will be displayed as 200 OK if the HTTP request succeeds. Otherwise, the error response could will be displayed.

Read

Postman read request

  1. Select the HTTP method as GET from the drop down.
  2. Enter the URI localhost:8080/sample in the URI field.
  3. Click the Send button to initiate the request. The response will be displayed in Respose section which is at the bottom of the window. The record that we created in the previous step will be displayed as the result.

Update

Postman update request

  1. Select the HTTP method as PUT from the drop down menu.
  2. Enter the URI as localhost/updatesample/1, where 1 is the id of the record which we intend to update with new values. All other options like Body, raw and JSON are all similar as we did for the Create functionality.
  3. In the body section, enter the record details as {“name”:”Ben”, “age”:”35″}. Note that we are updating the name from Ron to Ben and the age from 27 to 35.
  4. Click the Send button to send the request. After successful completion of the request, the updated record will be displayed in the Response section in JSON format.

Delete

Postman delete request

  1. Select the HTTP method as DELETE from the drop down menu.
  2. Enter the URI as localhost/deletesample/1, where 1 is the id of the record which we intend to delete.
  3. Click the Send button to initiate the request.
  4. After successful execution of the request, the Status of the response should be 200 OK. The response body will be empty.

Share this:

  • Facebook
  • Twitter
  • WhatsApp
  • Tumblr
  • Google
  • Pinterest