Skip to content

How To Test Http Request Using Postman

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool used for testing APIs. It allows you to send HTTP requests and receive responses, making it a valuable tool for developers and testers alike. This guide will walk you through the basics of using Postman to test HTTP requests.

1. Setting Up Your Environment

  • Download and Install Postman: Download Postman from the official website (https://www.postman.com/) and install it on your system.
  • Create a New Request: Once installed, open Postman and click on the “New” button. You’ll be prompted to create a new “Request” or “Collection.” For this guide, we’ll focus on individual requests.

2. Defining Your Request

  • Choose Your HTTP Method: Select the appropriate HTTP method for your request. Common methods include:
    • GET: Retrieves data from a server.
    • POST: Sends data to a server to create a new resource.
    • PUT: Updates an existing resource on a server.
    • DELETE: Removes a resource from a server.
  • Enter the URL: Paste the API endpoint URL you want to test in the “Enter request URL” field.
  • Set Headers (if necessary): Add any required headers to your request, such as authorization tokens or content type information.

Example:

Let’s say we want to test the GET request to retrieve a list of users from an API.

// URL: https://api.example.com/users
// Method: GET
// Headers:
// Content-Type: application/json
// Authorization: Bearer your_token

3. Sending Your Request and Inspecting the Response

  • Send the Request: Click the “Send” button to execute your request.
  • View the Response: Once the request is sent, Postman displays the response in different tabs:
    • Body: Shows the actual response data returned from the server.
    • Headers: Displays the headers sent by the server.
    • Cookies: Lists any cookies set by the server.
    • Test: Provides a space to write code for assertions and other test-related logic.

Example:

After sending the GET request to retrieve users, Postman might display a response like this:

Body:

[
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"role": "admin"
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane.smith@example.com",
"role": "user"
}
]

Headers:

Content-Type: application/json
Status: 200 OK

4. Working with Request Data (POST, PUT, DELETE)

For requests that involve sending data to a server (POST, PUT), Postman allows you to easily create and manage request bodies:

  • Body Tab: Select the “Body” tab and choose the data format for your request:
    • Raw: For plain text or code.
    • form-data: For sending form data with multiple key-value pairs.
    • x-www-form-urlencoded: For sending data encoded in URL format.
    • Binary: For uploading files.
    • JSON: For sending data in JSON format (commonly used for APIs).

Example: Creating a new user using a POST request.

URL: https://api.example.com/users Method: POST Headers:

Content-Type: application/json
Authorization: Bearer your_token

Body (JSON):

{
"name": "New User",
"email": "newuser@example.com",
"role": "user"
}

5. Assertions and Automated Testing

Postman includes a powerful testing framework to automate your API checks. You can write code in the “Test” tab to validate the response:

  • Assertions: Check specific properties of the response, such as status code, headers, and response body content.
  • Scripts: Write custom JavaScript code to perform more complex tests and automate testing workflows.

Example:

pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Validates response body", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.id).to.be.above(0);
});

6. Using Collections for Organization

For managing multiple API requests, Postman provides the concept of “Collections.” Collections allow you to:

  • Group Related Requests: Create collections to organize your API tests for specific projects or areas of your application.
  • Automate Workflows: Define the order of requests within a collection, allowing for end-to-end testing scenarios.
  • Share and Collaborate: Share collections with team members to promote consistent testing practices.

7. Exploring Advanced Features

Postman offers a rich set of advanced features for enhancing your testing process:

  • Environment Variables: Use environment variables to manage different configurations (e.g., API keys, URLs) for various environments (development, testing, production).
  • Data-Driven Testing: Use data from external sources like CSV files or JSON databases to parameterize your tests and run multiple test cases with different data.
  • Mock Servers: Create mock responses to simulate backend API behavior during development, allowing you to test frontend integrations before the backend is ready.

By understanding these features and using Postman effectively, you can significantly improve the quality and efficiency of your API testing.

API Testing Blog