Skip to content

How To Use Postman To Test Api

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool for testing APIs. It allows you to send requests to APIs, inspect responses, and automate tests. This guide will walk you through the basic steps of using Postman for API testing.

Setting up your Postman Environment

  1. Download and Install Postman: You can download Postman for free from their website: https://www.postman.com/
  2. Create a Workspace: Workspaces help organize your requests and collections.
  3. Create a New Request: Click the “New” button in the top left corner and select “Request”.

Sending Your First API Request

Let’s start with a simple GET request to fetch data from a public API, like the OpenWeatherMap API.

  1. Enter the API Endpoint: In the “Enter request URL” field, input the API endpoint, for example: https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY
  2. Replace “YOUR_API_KEY”: Obtain your free OpenWeatherMap API key and replace it in the URL.
  3. Select the HTTP Method: Choose “GET” from the dropdown menu.
  4. Send the Request: Click the “Send” button.

You’ll see the response from the API in the right panel. This includes the status code, headers, and response body.

Inspecting the Response

Postman provides multiple ways to examine the response:

  1. Status Code: The status code tells you if the request was successful. 200 means OK, while 400 indicates an error.
  2. Headers: The headers provide important information about the response, such as the content type, encoding, and server details.
  3. Body: The body contains the actual data returned by the API. You can view it as raw text, JSON, or XML.

Creating a Test for Your API

Postman allows you to write tests to verify the API behavior. You can add test scripts in the “Tests” tab.

Let’s create a test to verify the status code of the OpenWeatherMap API response:

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

This script uses the pm object to access Postman’s testing functions. The .response.to.have.status(200) assertion checks if the status code is 200.

Working with Collections & Environments

For complex APIs with multiple endpoints, it’s beneficial to organize your requests into Collections.

  1. Create a Collection: Click on “New” and select “Collection”. Give it a name, like “OpenWeatherMap-API”.
  2. Add Requests to the Collection: Drag and drop your requests into the collection.
  3. Add Variables: You can define variables in environments to store sensitive information like your API key, and use these variables in your requests.

Automating Your API Tests

Postman enables you to automate your API tests:

  1. Create a Collection Runner: Use the “Runner” Tab to create a runner, which executes the requests in your collection.
  2. Set Up Iterations and Data: You can define the number of iterations, introduce different data sets for each iteration, to test different scenarios.
  3. Exporting Results: Results can be exported in various formats like JSON, CSV, or HTML, and can be integrated with other tools.

Using Postman for Different API Testing Scenarios

Here are some common examples of how to use Postman for API testing:

1. Testing POST Requests with JSON Data:

  • Use the “Body” tab to select “raw” and paste your JSON payload.
  • Set the content-type header to “application/json”.

Sample Code:

// Create a JSON payload object
let data = {
"name": "John Doe",
"email": "john.doe@example.com"
};
// Set the header
pm.request.setHeaders({
"Content-Type": "application/json"
});
// Set the body
pm.request.body.raw(JSON.stringify(data));

2. Testing API Authentication with JWT Tokens:

  • Obtain the authentication token (JWT) through an authentication endpoint.
  • Store the JWT in a variable and use that variable in subsequent requests’ headers.

3. Testing API Endpoints with Dynamic Data:

  • Define variables like ‘userId’ in your environment, and use those variables in your requests.
  • Utilize the “Pre-request Script” to generate random data and set it to your request variables.

Essential Postman Concepts for Advanced Testing

  • Mocking APIs: Postman offers Mock Servers to simulate API responses for development and testing.
  • Data-Driven Testing: Use external data sources to test your API with various input parameters.
  • Integrations: Integrate Postman with other tools like GitHub, Jenkins, and Slack for CI/CD workflows.

Postman is a comprehensive platform for API testing. Start with the basics and explore its capabilities to build efficient and robust tests for your APIs.

API Testing Blog