Postman is a powerful tool for developing, testing, and documenting APIs. It provides a user-friendly interface to make HTTP requests, inspect responses, and automate testing. This section will guide you through the basics of using Postman for API testing.

Introduction to Postman

Key Features of Postman

  • User Interface: Intuitive UI for making requests and viewing responses.
  • Collections: Organize requests into collections for better management.
  • Environment Variables: Use variables to manage different environments (e.g., development, staging, production).
  • Automated Testing: Write tests using JavaScript to automate the validation of responses.
  • Documentation: Generate API documentation directly from collections.

Installing Postman

  1. Download Postman: Visit the Postman website and download the application for your operating system.
  2. Install Postman: Follow the installation instructions for your OS.
  3. Create an Account: Sign up for a free Postman account to save your work and access additional features.

Making Your First Request

Step-by-Step Guide

  1. Open Postman: Launch the Postman application.
  2. Create a New Request:
    • Click on the New button and select Request.
    • Name your request and add it to a new or existing collection.
  3. Set the Request Method and URL:
    • Choose the HTTP method (GET, POST, PUT, DELETE, etc.).
    • Enter the URL of the API endpoint you want to test.
  4. Add Headers (if necessary):
    • Click on the Headers tab and add any required headers (e.g., Content-Type, Authorization).
  5. Add Body (for POST/PUT requests):
    • Click on the Body tab and select the appropriate format (e.g., raw, form-data).
    • Enter the request payload.
  6. Send the Request:
    • Click the Send button to make the request.
    • Inspect the response in the lower pane.

Example: GET Request

GET https://jsonplaceholder.typicode.com/posts/1
  • Headers: None
  • Body: None

Response:

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit..."
}

Using Collections and Environments

Creating Collections

  • New Collection: Click on the New button and select Collection.
  • Add Requests: Drag and drop existing requests into the collection or create new ones within the collection.

Using Environment Variables

  • Create Environment: Click on the Environments tab and create a new environment.
  • Add Variables: Define variables (e.g., baseUrl, apiKey).
  • Use Variables in Requests: Replace static values with variables using the {{variableName}} syntax.

Example: Using Variables

GET {{baseUrl}}/posts/1
  • Environment Variable:
    • baseUrl = https://jsonplaceholder.typicode.com

Writing Tests in Postman

Basic Test Example

Postman allows you to write tests using JavaScript to validate responses.

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

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

pm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

Running Tests

  • Send Request: Click the Send button.
  • View Test Results: Check the Tests tab in the response section to see the results.

Automating Tests with Newman

Introduction to Newman

Newman is a command-line tool that allows you to run Postman collections. It is useful for integrating API tests into CI/CD pipelines.

Installing Newman

npm install -g newman

Running a Collection with Newman

newman run <path-to-collection.json> -e <path-to-environment.json>

Practical Exercise

Exercise: Create and Test a POST Request

  1. Create a New Request:
    • Method: POST
    • URL: https://jsonplaceholder.typicode.com/posts
    • Headers: Content-Type: application/json
    • Body:
      {
        "title": "foo",
        "body": "bar",
        "userId": 1
      }
      
  2. Send the Request: Click the Send button.
  3. Write Tests:
    pm.test("Status code is 201", function () {
        pm.response.to.have.status(201);
    });
    
    pm.test("Response has title", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.property("title");
    });
    
  4. Run the Tests: Verify that the tests pass.

Solution

  • Request URL: https://jsonplaceholder.typicode.com/posts
  • Headers: Content-Type: application/json
  • Body:
    {
      "title": "foo",
      "body": "bar",
      "userId": 1
    }
    
  • Tests:
    pm.test("Status code is 201", function () {
        pm.response.to.have.status(201);
    });
    
    pm.test("Response has title", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.property("title");
    });
    

Conclusion

In this section, you learned how to use Postman for API testing, including making requests, organizing collections, using environment variables, writing tests, and automating tests with Newman. Postman is a versatile tool that can significantly streamline the process of developing and testing APIs. In the next module, we will explore Swagger for API documentation.

© Copyright 2024. All rights reserved