Skip to content

How To Call Api Using Postman

API Testing Blog

Calling APIs using Postman: A Comprehensive Guide

Postman is a powerful tool for interacting with APIs. It simplifies the process of sending requests, analyzing responses, and testing API functionality. This guide will walk you through the essential steps of making API calls using Postman, from setting up your environment to analyzing the results.

Understanding API Requests and Responses

Before diving into Postman, it’s crucial to understand the basic concepts of API requests and responses.

  • API Request: This is a message sent to an API server, containing information about the desired action. It consists of several key components:

    • Method: Defines the desired action (e.g., GET, POST, PUT, DELETE).
    • URL: Specifies the API endpoint to interact with.
    • Headers: Additional information about the request, such as authentication data or content type.
    • Body: Data sent along with the request, often in JSON format.
  • API Response: The server’s reply to the request, usually containing data relevant to the requested action. The response also includes:

    • Status Code: Indicates the outcome of the request (e.g., 200 OK, 404 Not Found).
    • Headers: Additional information about the response, such as the content type.
    • Body: The data returned by the API, often in JSON format.

Setting Up Postman

Postman offers both a web application (https://www.postman.com/) and downloadable desktop versions for Windows, macOS, and Linux. You can choose the option that best suits your needs.

  1. Creating a New Request: Click on the “New” button in the top-left corner of the Postman interface and select “Request.”

  2. Defining the Request:

    • Method: Choose the appropriate HTTP method based on the desired action (e.g., GET for retrieving data, POST for creating new data).
    • URL: Enter the API endpoint URL in the address bar.

Building Your First API Call

Let’s use Postman to make a simple GET request to a public API that retrieves a list of users:

  1. Method: Select “GET” as the method.
  2. URL: Enter the following URL in the address bar: https://reqres.in/api/users?page=2
  3. Sending the Request: Click on the “Send” button (blue arrow).

Analyzing the Response

Postman displays the response from the API in a user-friendly format.

  1. Response Code: Look at the status code in the top-right corner. A 200 OK indicates a successful response.
  2. Response Body: Check the data received from the API in the “Body” tab.
  3. Headers: Explore the “Headers” tab to see additional information about the response.

Adding Headers to Your Request

Some APIs require specific headers to function correctly, such as Authorization headers for authentication.

  1. Adding a Header: Click on the “Headers” tab and click the “Add” button (plus sign).
  2. Setting Header Values: Enter the header name (e.g., “Authorization”) and its value.

Example:

  • Header Name: “Authorization”
  • Header Value: “Bearer your_access_token”

Sending Data with Request Bodies

To create new data or update existing data, use POST or PUT requests with a request body.

Example: Using a POST Request to Create a New User (JSON Body):

  1. Method: Select “POST” as the method.
  2. URL: Enter the URL for user creation (e.g., https://reqres.in/api/users).
  3. Body: Select “raw” and choose “JSON (application/json)” as the format.
  4. JSON Data: Enter the following JSON data in the body:
{
"name": "morpheus",
"job": "leader"
}
  1. Send: Click on the “Send” button.

Utilizing Environment Variables

Postman allows defining variables to store dynamic values like API keys or base URLs, making your requests reusable across different environments.

  1. Create a New Environment: Click on the “Environments” tab and click “New.”
  2. Add Variables: Define your variables with names (e.g., “API_KEY”) and their values.
  3. Using Environment Variables: In your request URL or headers, use the syntax {{variable_name}} to reference the variable.

Example:

  • Variable Name: API_KEY
  • Value: “your_api_key”
  • Request URL: https://api.example.com/v1/users?key={{API_KEY}}

Testing API Functionality with Assertions

Postman offers a powerful feature for verifying the expected behavior of your APIs using assertions. These allow you to test the response against your desired conditions.

  1. Adding an Assertion: Click on the “Tests” tab and add a new assertion using the built-in test script editor.
  2. Assertion Examples:
    • Verify Status Code: pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
    • Check Response Body: pm.test("Name is 'morpheus'", function () { pm.expect(pm.response.json().name).to.eql("morpheus"); });

Saving and Reusing Requests

Postman allows you to save your API calls for easy reuse and organization.

  1. Saving a Request: Click on the “Save” button (folder icon) and assign a name and description to your request.
  2. Creating a Collection: Organize your requests into collections for improved management and collaboration.
  3. Running Collections: Postman allows you to run multiple requests in a collection sequentially or in parallel.

Conclusion

Postman is a powerful and user-friendly tool for interacting with APIs. It simplifies the process of sending requests, analyzing responses, and testing API functionality. Utilizing features like headers, request bodies, environments, and assertions allows you to effectively work with APIs, making Postman an essential tool for developers and testers alike.

API Testing Blog