Skip to content

How To Use Postman To Test Web Service

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool used for testing and interacting with APIs. It provides a user-friendly interface for sending requests, viewing responses, and managing API documentation. This guide will walk you through the basics of using Postman for API testing with practical examples.

1. Setting Up Postman

  • Download & Install: Head over to https://www.postman.com/ and download the Postman app for your operating system. Install it and launch the application.
  • Create a Workspace: A workspace allows you to organize your API testing activities, share them with teammates, and manage your collection of requests, environments, and documentation.
  • Create a Collection: Collections in Postman are like folders where you can group related requests for a specific API. Create a new collection by clicking the “New” button in the left sidebar and then select “Collection.”

2. Sending a Simple GET Request

Let’s start with a simple GET request to fetch data from an API endpoint. We’ll use the JSON Placeholder API, a popular resource for testing.

Step 1: Build your request

  1. On the Postman interface, click the “New” button (or press Ctrl/Cmd+N). Select “Request.”
  2. In the request builder, set the “Method” to “GET.”
  3. In the “Enter request URL” field, type the API endpoint: https://jsonplaceholder.typicode.com/todos/1

Step 2: Send the request and view the response

  1. Click the “Send” button (or press Enter).
  2. Postman will display the response in the “Body” tab. You should see the JSON data representing a single “todo” item.

Sample Code:

{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}

3. Adding Headers and Authorization

Many APIs require you to send headers or use authorization to access their resources. Let’s send a request with a Content-Type header.

Step 1: Add Header

  1. In the request builder, click the ‘Headers’ tab.
  2. Click “Add” and enter the key-value pair: Content-Type: application/json.

Step 2: Send the request and view the response

  1. Click “Send” to see the response.

Sample Code (Headers Tab):

Content-Type: application/json

4. Sending POST, PUT, and DELETE Requests

Postman supports all common HTTP methods. For example, to create a new “todo” item, we’ll use a POST request:

Step 1: Build your POST request

  1. In the request builder, set the “Method” to “POST.”
  2. Keep the endpoint: https://jsonplaceholder.typicode.com/todos
  3. In the “Body” tab, select “raw” and choose “JSON (application/json)” from the dropdown.
  4. Paste the following JSON data:
{
"userId": 1,
"title": "New todo item",
"completed": false
}

Step 2: Send the request and view the response

  1. Click “Send.”
  2. You should see a response containing the newly created “todo” item.

For PUT and DELETE requests, follow similar steps, updating the request method and the endpoint based on the API documentation.

5. Using Environments and Variables

Environments in Postman allow you to manage different configurations for your API requests. This is especially helpful when working with multiple environments (development, testing, production), or when you need to use different API keys or base URLs.

Step 1: Create an Environment

  1. Click the “Manage Environments” button at the top of the screen.
  2. Click “Add” and give your environment a name (e.g., Development).
  3. In the “Variables” section, add variables like:
    • baseUrl: https://jsonplaceholder.typicode.com
    • apiKey: (Replace with your actual API key if applicable)

Step 2: Use Environment Variables

  1. In your request builder, click the “Variables” tab.
  2. Now, instead of hardcoding the base URL, use the {{baseUrl}} variable in the “Enter request URL” field: {{baseUrl}}/todos/1.
  3. Similarly, you can use the {{apiKey}} variable where needed.

Step 3: Switch Environments

You can easily switch between different environments by selecting them from the drop-down list in the top-right corner of Postman.

6. Test Your API with Assertions

To ensure your API functions correctly, Postman offers built-in assertions. Assertions help automate the validation of your response data.

Step 1: Add Assertions

  1. After sending your request, click the “Tests” tab.
  2. Click “Add a Test” and use the test scripting language provided (JavaScript).
  3. For example, to check if the response status code is 200:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

Step 2: Run Tests

  1. Click the “Send” button to run your request and execute the tests.
  2. The results will be displayed in the “Tests” tab, indicating whether the tests passed or failed.

7. Using Collections and Workflows

Collections in Postman allow you to group related requests. They can be used to automate testing workflows, making it easier to run a series of requests in a specific order.

Step 1: Create a Collection

  1. Click the “New” button and select “Collection.”
  2. Give your collection a name (e.g., Todo API Tests).

Step 2: Add Requests to the Collection

  1. Drag and drop your existing requests into the collection.
  2. Or, you can create new requests directly within the collection.

Step 3: Run Collection Runner

  1. Click the “Runner” button (looks like a play icon) at the top of the collection.
  2. You can set up settings like environment, iterations, data variables, and more.
  3. Click the “Run” button to execute the entire workflow.

Step 4: Analyze Results

  1. After the collection runs, Postman provides detailed reports showing the results of each request and test.

Conclusion

Postman is an invaluable tool for API testing, providing a comprehensive environment for building requests, managing environments, and running tests. By using Postman effectively, you can streamline your API testing process, ensuring your APIs function correctly and meet the desired requirements.

API Testing Blog