Skip to content

How To Use Rest Api In Postman

API Testing Blog

Getting Started with REST API Testing in Postman

Postman is a powerful tool for interacting with and testing REST APIs. This guide will walk you through the basics of using Postman, covering sending requests, analyzing responses, and common scenarios you’ll encounter during API testing.

1. Setting up Postman

  • Download and Installation: Head over to https://www.postman.com/ and download the Postman app for your operating system. Installation is straightforward and follows typical software installation processes.

  • Creating a Workspace: Workspaces organize your requests, collections, environments, and other Postman artifacts. Create a workspace to keep your testing projects organized.

  • Building Your First Request:

    • Click on the “New” button in the Postman interface.
    • Choose “Request” to create a new request.

2. Understanding the Request Components

  • HTTP Method: Select the appropriate HTTP method for your API interaction (GET, POST, PUT, PATCH, DELETE).
  • URL: Enter the correct API endpoint URL.
  • Headers: Add necessary headers, like Content-Type, Authorization, or custom headers specific to the API.
  • Body:
    • Form Data: Use this for key-value pairs sent as part of a form.
    • JSON: Send structured data in JSON format.
    • Raw: Send data in raw format (text, HTML, etc.).
    • Binary: Upload files to the API.

3. Sample API Interaction (GET request)

Scenario: Retrieving a list of users from a hypothetical API.

API Endpoint: https://api.example.com/users

Steps:

  1. Method: Select “GET” from the dropdown.
  2. URL: Enter https://api.example.com/users in the URL field.
  3. Headers: You might need to add specific headers. For example:
    • Content-Type: application/json (for JSON responses)
    • Authorization: (if the API requires authentication).
  4. Send Request: Click “Send.”

Response:

  • Status Code: Look for a successful response code (e.g., 200 OK).
  • Response Body: The API’s response data will be displayed in the response body.

Example Code (JSON Response):

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

4. Using Authorization in Postman

Many APIs require authentication to access data. Postman supports several authentication methods:

  • Basic Auth: Enter username and password directly.
  • Bearer Token: Include a bearer token in the Authorization header.
  • API Key: Provide the API key in the header.
  • OAuth 2.0: A more complex flow for authorization, often used with social logins.

To set up authorization:

  1. Click the “Authorization” tab in the request.
  2. Select the appropriate method (e.g., “Bearer Token”).
  3. Enter your credentials.

5. Sending a POST Request

Scenario: Creating a new user using a POST request.

Steps:

  1. Method: Select “POST.”
  2. URL: https://api.example.com/users
  3. Headers: Include Content-Type: application/json.
  4. Body:
    • JSON:
      {
      "name": "Charlie",
      "email": "charlie@example.com"
      }
  5. Send Request: Click “Send.”

Successful Response:

  • Status Code: 201 Created.
  • Response Body: The API might provide details about the newly created user.

6. Testing with Collections

  • Organize API Tests: Collections group requests together. This makes it easy to execute a series of tests for related API endpoints.
  • Add Requests: Drag and drop requests into a collection.
  • Create Environments: Environments store variables that change based on testing environments (for instance, different API endpoints for development vs. production).
  • Running Tests: Execute requests within a collection in a defined order.

7. Working with Assertions

  • Verify Results: Assertions let you check if the response matches your expectations, making sure the API is behaving correctly.
  • Examples:
    • Status Code: Assert that the response code matches the expected value (e.g., 200 for success).
    • Response Body: Check for specific text or data in the JSON response.
    • Response Time: Ensure the API responds within a reasonable time frame.

Example Assertion:

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

8. Using Pre-request Scripts

  • Automate Tasks: Pre-request scripts allow you to run code before a request is sent (for tasks like generating dynamic data or preparing headers).
  • Example: Generating a random user ID in a POST request:
const userId = Math.floor(Math.random() * 1000); // Generate a random ID
pm.environment.set("userId", userId); // Store the ID in an environment variable

9. Testing your API Using Postman’s Features

Postman offers tools for comprehensive testing:

  • Automated Tests: Create test suites with collections.
  • The Postman Runner: Execute your tests in batch mode.
  • Test Reports: Generate reports showing test results and pass/fail rates.
  • Debugging: Troubleshoot your API using the console and debugger.

10. Final Tips

  • Explore the Postman Interface: Postman’s documentation and tutorials are a valuable resource.
  • Use Collections: Keep your tests organized and reusable.
  • Start with Simple Tests: Build complexity gradually as you gain more experience.

By following the steps outlined in this guide and utilizing Postman’s features, you can effectively test your REST APIs, ensuring their reliability and functionality.

API Testing Blog