Skip to content

How To Test Web Api Service Using Postman

API Testing Blog

A Comprehensive Guide to API Testing with Postman

Postman is a powerful tool that simplifies the process of testing web APIs. This guide will walk you through the fundamentals of API testing with Postman, demonstrating how to send requests, verify responses, and streamline your workflow.

Getting Started with Postman

Before we dive into testing, let’s set up our environment:

  1. Download and Install Postman: You can download Postman for free from https://www.postman.com/.
  2. Create a New Workspace: Workspaces in Postman help you organize your API testing projects.
  3. Create a New Request: Click the “New” button, then select “Request” to begin crafting your first API test.

Sending Requests

1. Defining the Request:

  • HTTP Method: Choose the appropriate HTTP method (GET, POST, PUT, DELETE, etc.) based on the API endpoint you’re interacting with.
  • URL: Enter the complete URL of your API endpoint.
  • Headers: Add any necessary headers for authorization (like API keys or tokens) or content type specifications.
  • Body: Structure your request body depending on the API’s requirements. You can use different formats for the body, including JSON, XML, or URL-encoded data.

2. Example Request:

Let’s say we’re testing an API endpoint to get a list of products.

Request:

Method: GET
URL: https://api.example.com/products
Headers:
Authorization: Bearer YOUR_ACCESS_TOKEN

3. Sending the Request:

Once you’ve configured your request, click the “Send” button in Postman to execute the API call.

Inspecting the Response

After sending your request, Postman displays the response from the API:

1. Response Status Code: Key to understanding the success or failure of the request. Common codes include 200 (OK), 400 (Bad Request), 404 (Not Found), and 500 (Server Error).

2. Response Body: Contains the data returned by the API. This could be JSON, XML, plain text, or any other format depending on your API.

3. Response Headers: Provide additional information about the response, such as content type and encoding.

Testing and Verification

Postman offers essential features to facilitate comprehensive API testing.

1. Assertions:

  • Test Tab: Postman’s “Tests” tab lets you write code to verify the response against expectations. You can use Javascript code to assert specific conditions, such as:
    • Status Code: Ensure the response code matches the expected value.
    • Body Content: Verify the presence of specific data fields or values in the response body.
    • Headers: Check if the response contains the expected headers.

Example Assertion:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

2. Variables:

  • Environment Variables: Store reusable values like API keys, base URLs, or test data. This helps you manage different test environments and avoids redundancy.
  • Data Variables: Define collections of data for testing various scenarios.

Example with Environment Variable:

// Define an environment variable named "baseUrl"
pm.environment.set("baseUrl", "https://api.example.com");
// Send requests using the dynamic baseUrl
pm.sendRequest(`${pm.environment.get("baseUrl")}/products`, function (err, res) {
// ...your test code ...
});

Creating Test Collections

1. Organization:

  • Collections: Postman Collections help you organize requests related to a specific API or functionality.
  • Folders: Group your requests within collections into folders for better structure.

2. Chaining Requests:

  • Test Flows: Collections allow you to define logical sequences of requests to simulate complex workflows and interactions.
  • Data Passing: You can use variables to pass data between requests within a collection.

Advanced Features

Postman provides a wide range of tools to enhance your API testing:

  • Mock Servers: Create mock APIs for testing purposes without relying on a fully developed backend.
  • Webhooks: Create automated workflows to trigger tests based on predefined events.
  • Runner: Execute test collections in bulk for scalability and comprehensive analysis.
  • Integrations: Connect with other tools like CI/CD platforms for continuous integration and automation.

Practical Examples:

Here are examples of how you can use Postman to test different scenarios:

Scenario 1: Creating a User (POST Request):

  1. Method: POST
  2. URL: https://api.example.com/users
  3. Headers: Content-Type: application/json
  4. Body:
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com"
}

Scenario 2: Fetching User Details (GET Request):

  1. Method: GET
  2. URL: https://api.example.com/users/123

Scenario 3: Updating User Information (PUT Request):

  1. Method: PUT
  2. URL: https://api.example.com/users/123
  3. Body:
{
"firstName": "Jane",
"lastName": "Doe",
"email": "jane.doe@example.com"
}

Scenario 4: Deleting a User (DELETE Request):

  1. Method: DELETE
  2. URL: https://api.example.com/users/123

Scenario 5: Creating a Product with Validation (POST Request):

  1. Method: POST
  2. URL: https://api.example.com/products
  3. Headers:
    • Content-Type: application/json
  4. Body:
{
"productName": "Laptop",
"productCategory": "Electronics",
"price": null // Intentionally leaving price field empty for validation error
}

Assertions:

  • Status Code is 400: The price field is required for the product object, so the API should return a 400 Bad Request error.
  • Response Body contains an error message: The response should include an error message explaining that the price field is missing.

By utilizing Postman’s features, you can test your web API services efficiently and comprehensively, ensuring robustness and quality throughout the development lifecycle.

API Testing Blog