Skip to content

How To Test Rest Web Service Using Postman

API Testing Blog

Getting Started with Postman for REST API Testing

Postman is a powerful tool for interacting with APIs, offering a user-friendly interface and comprehensive features for testing RESTful web services. Let’s dive into the essentials of using Postman for API testing.

1. Download and Install Postman

Begin by downloading Postman from https://www.postman.com/. Choose the appropriate version for your operating system and follow the installation instructions.

2. Understanding the Postman Interface

Once installed, open Postman. You’ll be greeted with a workspace that includes:

  • Navigation Bar: This area offers options for managing workspaces, collections, environments, and more.
  • Request Builder: The core area where you construct your API requests.
  • Response Pane: Displays the API response after sending a request.

3. Constructing Your First API Request

Let’s begin by testing a simple GET request to fetch data from a weather API.

Sample API Endpoint: https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY

Steps:

  1. Enter the Request URL: In the “Enter request URL” field, paste the API endpoint.
  2. Select the HTTP Method: Choose “GET” from the dropdown.
  3. Send the Request: Click the “Send” button.

You’ll see the API response displayed in the Response Pane.

4. Working with Headers

Many APIs require specific headers to authenticate or convey additional information.

Example: Authorization Header

  1. Add a Header: Go to the “Headers” tab in the Request Builder.
  2. Enter Key and Value: Add a new header with “Authorization” as the key and a valid authentication token as the value.

5. Sending Different HTTP Requests

Postman supports various HTTP methods, each serving different purposes:

  • GET: Retrieves data from the API.
  • POST: Creates a new resource.
  • PUT: Updates an existing resource.
  • DELETE: Deletes a resource.
  • PATCH: Partially updates a resource.

Sample POST Request:

API Endpoint: https://reqres.in/api/users Request Body (JSON):

{
"name": "Morpheus",
"job": "Leader"
}

Steps:

  1. Set HTTP Method: Select “POST” from the dropdown.
  2. Add Body: Switch to the “Body” tab.
  3. Select “raw” and choose “JSON” as the format.
  4. Paste the JSON body into the editor.
  5. Send the Request: Click “Send.”

6. Validation and Assertions

Postman’s “Tests” tab enables you to write automated tests for your API requests.

Example: Verifying Response Status Code

  1. Add Tests: Go to the “Tests” tab in the Request Builder.

  2. Insert Code Snippet: Paste the following JavaScript code:

    pm.test("Status code is 200", function() {
    pm.response.to.have.status(200);
    });
  3. Send the Request: Click “Send.”

The test will verify whether the response status code is 200, as expected.

7. Creating Collections and Environments

Collections: Group related requests for better organization and reusability.

Environments: Manage and store API credentials, base URLs, and other variables for easy switching between different testing environments.

Example: Building a Collection:

  1. Create Collection: Go to the “Collections” tab and create a new collection with a relevant name (e.g., “Weather API”).
  2. Add Requests: Add the individual requests you’ve constructed earlier (GET, POST, etc.) to the collection.
  3. Run Collection: Use the “Run” button to execute all requests in the collection.

8. Sharing and Collaboration

Postman allows you to share collections, environments, and even entire workspaces with your team members, fostering collaboration and streamlined testing processes.

Sharing a Collection:

  1. Export Collection: Go to the “Share” button in the collection.
  2. Choose Option: Select “Export” and choose a file format (e.g., JSON).
  3. Share File: Share the exported file with your team members.

Conclusion

Postman simplifies API testing, providing a comprehensive set of tools for sending requests, verifying responses, and generating documentation. By mastering its core features, you can effectively test your RESTful web services and ensure their robustness and reliability.

API Testing Blog