How to Use Postman for API Testing: A Guide for Beginner’s Tutorial

API testing is an essential part of software development that ensures your APIs are working as expected and delivering the correct data. Postman is a powerful tool for API testing, allowing you to send requests, analyze responses, and automate tests efficiently. In this step-by-step guide, we’ll show you how to get started with Postman and run basic API tests. Whether you’re a developer or tester, this tutorial will give you a solid foundation for API testing using Postman.


Step 1: What is Postman?

Before diving into the tutorial, let’s define Postman and its purpose in API testing.

  • Postman is an API testing tool that simplifies working with APIs by offering an intuitive interface for sending requests and receiving responses.
  • It supports a wide range of HTTP methods (GET, POST, PUT, DELETE, etc.), making it ideal for REST API testing.
  • Why use Postman?
    • Easy to set up and use.
    • Great for both manual and automated API testing.
    • Allows team collaboration and sharing of collections.

Step 2: Setting Up Postman

2.1 Installing Postman

  • Head over to the Postman download page and install the app for your operating system (Windows, macOS, or Linux).
  • Open Postman once installed, and either sign up for a Postman account or use it without an account (optional for basic usage).

2.2 Familiarizing Yourself with the Interface

When you open Postman, you’ll see the main dashboard, which includes:

  • Workspaces: Organize your requests and tests by project.
  • Collections: Group multiple API requests and tests for easy access.
  • Request Builder: The main area where you’ll create, modify, and send API requests.

Step 3: Making Your First API Request

Let’s begin by sending a simple GET request to retrieve data from an API.

3.1 Sending a GET Request

  1. Create a new request:
    • Click on New in the top left and choose Request.
    • Name your request (e.g., “Get User Info”) and save it in a new collection.
  2. Enter the request URL:
  3. Choose the HTTP method:
    • Select GET from the dropdown list (this is the default method for fetching data).
  4. Click Send:
    • Postman will display the response from the server in the lower section of the screen.

3.2 Understanding the Response

  • Status Code: You’ll see a 200 OK status code if the request was successful.
  • Response Body: The main content (in JSON format) will appear in the body, displaying the data returned by the API.
  • Response Time and Size: This shows how quickly the server responded and the size of the data returned.

Step 4: Sending POST Requests

Now, let’s create a POST request to send data to an API.

4.1 Creating a POST Request

  1. Create a new request:
    • Click New, choose Request, and name it (e.g., “Create New User”).
  2. Enter the POST URL:
  3. Choose the HTTP Method:
    • Change the HTTP method to POST in the dropdown.

4.2 Adding Data to the Request Body

For POST requests, you’ll need to include data in the request body.

  1. In the Body tab below the URL field, choose raw and set the format to JSON.
  2. Enter the JSON data you want to send, for example: json { "title": "foo", "body": "bar", "userId": 1 }

4.3 Sending the POST Request

  • Click Send and view the response in the lower panel.
  • A 201 Created status code indicates that the request was successful, and the API created a new resource.

Step 5: Adding Headers and Query Parameters

APIs often require headers or query parameters to work properly.

5.1 Adding Headers

Headers provide additional information to the API (e.g., authentication tokens).

  1. In the Headers tab of your request, add a key-value pair. For example:
    • Key: Content-Type
    • Value: application/json
  2. If an API requires authentication, you can add a header for the token:
    • Key: Authorization
    • Value: Bearer <your_token_here>

5.2 Adding Query Parameters

Query parameters are appended to the URL to filter or customize the response.

  1. Use the Params tab to add query parameters.
    • For example, to limit the number of users in the response from /users, add a parameter like limit=5.
  2. Postman will automatically append the query string to the URL.

Step 6: Writing Tests in Postman

Postman allows you to write automated tests that run after receiving the API response. Let’s write a basic test to check if our API response is correct.

6.1 Adding Test Scripts

  1. In your request, click the Tests tab.
  2. Write a simple JavaScript test, for example: javascript pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test("Response time is less than 500ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); });

6.2 Running Tests

  • After writing your test, click Send.
  • Postman will automatically run the tests, and you’ll see the test results in the Test Results tab below the response.

Step 7: Saving Requests and Collections

Collections allow you to save related API requests and tests, making it easier to manage larger projects.

7.1 Creating a Collection

  1. Click on Collections in the sidebar.
  2. Click New Collection and give it a name (e.g., “My API Tests”).
  3. Add individual requests (GET, POST, etc.) to this collection for easy access later.

7.2 Running Collections with Newman

  • Postman allows you to run entire collections using Newman, a command-line tool.
  • You can automate API testing by integrating Newman with CI/CD pipelines like Jenkins or GitHub Actions.

Step 8: Using Postman Environments

Environments in Postman allow you to run the same requests with different data. For example, you may have separate environments for Development, Testing, and Production.

8.1 Creating an Environment

  1. Click Environment in the top-right corner and select Manage Environments.
  2. Add a new environment, such as Development.
  3. Define variables (e.g., base_url, auth_token) that can change across environments.

8.2 Using Variables in Requests

  1. In your request, use double curly braces {{}} to refer to environment variables:
    • For example, replace the URL with {{base_url}}/users.
  2. Switch between environments to run the same requests against different servers.

Post Comment