Skip to content

How To Use Postman 7

API Testing Blog

Getting Started with Postman 7: A Comprehensive Guide for API Testing

Postman is a powerful tool for API testing, allowing you to send requests, analyze responses, and automate testing workflows. This guide will walk you through the fundamentals of using Postman 7, covering everything from basic requests to advanced features.

1. Setting Up Your Environment

Before diving into API testing, you need to set up your Postman environment:

1.1 Download and Install Postman:

  • Head over to the official Postman website (https://www.postman.com/) and download the latest version of Postman for your operating system.
  • Follow the installation instructions.

1.2 Create a Workspace:

  • Once installed, launch Postman.
  • Click on the “Workspaces” icon in the left sidebar.
  • Select “Create Workspace.”
  • Choose a suitable name for your workspace.
  • Decide whether to make it private or public.

1.3 Import Your API Documentation:

  • Many APIs provide documentation in formats like OpenAPI/Swagger or Postman Collections.
  • In Postman, click on “Import” in the top right corner.
  • Select “From Link” or “Upload File” to import your documentation.

2. Sending Your First Request

Let’s begin with a simple GET request to retrieve data from an API:

2.1 Access the Request Builder:

  • Click on the “New” button in the top left corner to create a new request.

  • In the request builder window, you’ll see various sections:

    • Method: Select the HTTP method (e.g., GET, POST, PUT, DELETE).
    • URL: Enter the API endpoint’s URL.
    • Headers: Used to send additional information with your request.
    • Body: Contains request parameters (for POST, PUT, and DELETE methods).

2.2 Example: Fetching Data from a Random User API:

  • Set Method: GET
  • Set URL: https://randomuser.me/api/?results=5
  • Click on “Send”.

2.3 Understanding the Response:

  • Postman displays the response from the API in different tabs:
    • Body: The raw data returned by the API. It could be JSON, XML, or other formats.
    • Headers: HTTP headers sent back by the server.
    • Cookies: Any cookies sent by the server.
    • Test: This tab allows you to write tests for your API response (more on this later).

3. Working with Environment Variables

Environment variables are dynamic values that allow you to manage different API environments (e.g., development, testing, production) without modifying your requests manually:

3.1 Creating Environment Variables:

  • Click on the “Environments” icon in the left sidebar.
  • Click on “Add” to create a new environment.
  • Give it a name (e.g., dev, test, prod).
  • Add variables:
    • Key: The variable name (e.g., api_base_url).
    • Value: The corresponding value for your environment (e.g., https://api.example.com).

3.2 Using Variables in Requests:

  • In your request URL or headers, use double curly braces {{ }} around your variable name:

    URL: {{api_base_url}}/users
  • When you send a request, Postman automatically replaces the variable with the value defined in your active environment.

4. Building Tests for your API

Testing validates your API behavior and ensures functionality. Postman’s built-in testing framework makes it easy:

4.1 The “Tests” Tab:

  • In the response tab, go to “Tests.”
  • Postman uses JavaScript code for writing tests.

4.2 Sample Test Code:

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

4.3 Running Tests:

  • Send your request.
  • The “Tests” tab will display the results of your test assertions, indicating success or failure.

5. Mastering Request Methods and Parameters

Postman supports all common HTTP methods (GET, POST, PUT, DELETE) and various ways to send data:

5.1 GET Request:

  • Used to retrieve data from the server.
  • Example: GET /users (fetches a list of users)

5.2 POST Request:

  • Used to create new resources on the server.
  • Example: POST /users (creates a new user)

5.3 PUT Request:

  • Used to update existing resources.
  • Example: PUT /users/1 (updates the user with ID 1)

5.4 DELETE Request:

  • Used to delete resources from the server.
  • Example: DELETE /users/1 (deletes the user with ID 1)

5.5 Body Parameters:

  • Used to send data with POST, PUT, and DELETE requests.
  • In the Request Builder, switch to the “Body” tab.
  • Choose the appropriate type:
    • form-data: For sending key-value pairs.
    • x-www-form-urlencoded: Similar to form-data but encodes data differently.
    • raw: Allows sending raw text, JSON, XML, etc.
    • binary: For sending binaries (files, images).

5.6 Example POST Request:

// Request Body (JSON):
{
"name": "John Doe",
"email": "john.doe@example.com"
}

5.7 Query Parameters:

  • Passed as part of the URL to filter or modify the request.

  • Use question marks (?) followed by key-value pairs separated by ampersands (&):

    URL: /users?page=2&limit=10

6. Automating Your Workflows: Collections and Runners

Postman offers tools to automate repetitive tasks:

6.1 Collections:

  • Organize and group a set of related requests into a collection.
  • Collections streamline testing by keeping requests together.
  • You can also add descriptions and documentation to collections for better organization.

6.2 Runners:

  • Used to execute collections in a defined sequence.
  • Customize how requests are run (e.g., set environment, iterate through data).
  • Set up automated tests to run collections at specific intervals.

Example: Testing a User API Endpoint:

  1. Create a Collection:
    • Name your collection “User API Tests.”
    • Add requests for creating, fetching, updating, and deleting users.
  2. Add Tests to Requests:
    • Within each request, write tests to verify expected responses and data.
  3. Create a Runner:
    • Name it “User API Test Runner.”
    • Select your User API Tests collection.
    • Define the environment to use.
    • (Optional) Set iterations and delays.

7. Advanced Features and Tips

7.1 Mocking APIs:

  • Create mock responses for APIs that are in development or not accessible.
  • Simulate API behavior and test your application without relying on the actual API.

7.2 Pre-request Scripts:

  • Write JavaScript code to execute before each request.
  • Useful for setting up variables, modifying headers, or performing other actions.

7.3 Post-request Scripts:

  • Run JavaScript after a request has been made.
  • Ideal for extra validations, log data, or perform conditional operations based on the response.

7.4 Data-driven Testing:

  • Test different scenarios with multiple data sets.
  • Use data files or external databases to drive your test cases.

7.5 Sharing and Collaboration:

  • Collaborate with team members by sharing collections, environments, and workspaces.
  • Postman offers different sharing options (public, private, team-specific).

8. Conclusion:

This guide has provided you with a solid foundation for using Postman 7 to test APIs effectively. Remember to experiment, explore the tool’s advanced features, and continuously learn and improve your API testing skills. Happy testing!

API Testing Blog