Skip to content

How To Use Postman To Test Api Calls

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool for testing APIs, offering a user-friendly interface and robust features to streamline your testing process. Let’s explore how to use Postman effectively for API testing.

Setting Up Your Environment

  1. Install Postman: Download and install Postman from https://www.postman.com/downloads/.
  2. Create a Workspace: Workspaces help you organize your API testing activities. Click the “Workspaces” icon in the left sidebar and create a new workspace.
  3. Import or Create Collections: Collections in Postman are a group of requests organized into folders. If you have a collection of API requests defined in a file, you can import it. Otherwise, create a new collection for your specific API.

Sending Your First API Request

  1. Navigate to the Collection: Open the collection where you want to add a new request.
  2. Create a Request: Click the “Add Request” button (plus icon) to create a new request within your collection.
  3. Set the Request Details:
    • Method: Select the HTTP method (GET, POST, PUT, DELETE, etc.) for your request.
    • URL: Enter the full URL of the API endpoint you want to test.
    • Headers: Add any required headers for your API (e.g., Authorization, Content-Type).

Example:

Let’s consider the example of a simple GET request to fetch a list of users from a hypothetical API endpoint:

Request:

GET https://api.example.com/users

Headers:

Authorization: Bearer YOUR_API_TOKEN
Content-Type: application/json
  1. Send the Request: Click the “Send” button to execute the request.

Analyzing the Response

Postman displays the response from the API in the “Response” tab:

  • Status Code: The HTTP status code indicates the success or failure of the request (e.g., 200 - OK, 404 - Not Found).
  • Headers: The response headers provide information about the response.
  • Body: The response body contains the actual data returned by the API.

Example:

Response Status Code: 200 OK

Response Body:

[
{
"id": 1,
"name": "Alice",
"email": "alice@example.com"
},
{
"id": 2,
"name": "Bob",
"email": "bob@example.com"
}
]

Adding Assertions for Validation

Assertions allow you to automatically verify the correctness of the API response based on predefined criteria.

  1. Add Assertions: In the “Tests” tab, click the ”+” button to add a new assertion.
  2. Define Assertions: Select the type of assertion (e.g., status code, response body content, headers) and specify the expected values.

Example:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response body contains Alice", function () {
pm.expect(pm.response.text()).to.include("Alice");
});

Testing Different API Scenarios

Postman allows you to explore various scenarios for your API:

  • Parameterizing Requests: Easily pass different values to your API endpoint by defining variables in your requests.
  • Data-Driven Testing: Create data-driven tests by importing data from external sources (CSV, JSON) and using it to run multiple variations of your requests.
  • Environment Variables: Store API credentials and other sensitive information in environment variables, making your tests reusable across different environments.

Automating API Tests

Postman supports automation using the Newman CLI tool or the Postman Collection Runner feature:

  • Newman: A command-line tool for running Postman collections. You can schedule Newman runs using CI/CD pipelines.
  • Postman Collection Runner: A feature available within the Postman app for executing collections and viewing results.

Example: Testing a POST Request with Authorization

Scenario: Creating a new user using a POST request with basic authentication.

Request:

POST https://api.example.com/users
Authorization: Basic YOUR_BASIC_AUTH_CREDENTIALS
{
"name": "Charlie",
"email": "charlie@example.com"
}

Assertions:

pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response body contains Charlie", function () {
pm.expect(pm.response.text()).to.include("Charlie");
});

Note: Replace YOUR_BASIC_AUTH_CREDENTIALS with your actual basic authentication credentials.

By following these steps and incorporating the examples provided, you can effectively utilize Postman to test your API calls. This comprehensive guide empowers you to send requests, analyze responses, create assertions, and automate your API testing workflow for efficient and reliable quality assurance. Remember to adapt the examples to your specific API and testing requirements.

API Testing Blog