Skip to content

How To Use Postman To Test Web Api

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool that simplifies API testing. It allows you to send requests to your API, inspect responses, test different scenarios, and automate your workflow. Let’s dive into how to use Postman to effectively test your web APIs.

1. Setting Up Postman

  • Download and Install: Download Postman from the official website (https://www.postman.com/). It’s available for Windows, Mac, and Linux.
  • Create a Workspace: Workspaces help organize your API testing tasks. Click “Create Workspace” and choose a suitable name.

2. Creating Your First Request

  • New Request: Click the “New” button, then “Request” to open a new request window.
  • Request Method: Select the appropriate HTTP method (GET, POST, PUT, DELETE, etc.) based on the API endpoint you’re working with.
  • Enter URL: Paste the API endpoint URL into the “Enter request URL” field.
  • Headers: For specific requests, add headers by clicking on the “Headers” tab. Common headers include “Content-Type” (e.g., “application/json”).

Example: Making a GET Request

Let’s say we want to test a GET endpoint to fetch a list of users from your API:

  1. Request Method: GET
  2. URL: https://api.example.com/users
  3. Headers:
    • Content-Type: application/json

Code:

GET https://api.example.com/users
Content-Type: application/json

3. Sending Requests

  • Send Button: Click the “Send” button to execute your request.

Response:

Postman displays the response from your API in the “Response Body” area. This includes the status code, response headers, and the actual data returned by the API.

4. Inspecting Responses

  • Status Code: The status code (e.g., 200 OK, 404 Not Found) indicates the success or failure of the request.
  • Response Headers: These headers provide additional information about the response.
  • Response Body: This contains the data returned by the API.

Example Response for GET request to https://api.example.com/users:

Status code: 200 OK Response Headers:

Content-Type: application/json

Response Body:

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

5. Testing with Parameters

  • URL Parameters: For APIs that accept parameters in the URL, add them using the “Params” tab.
  • Body Data: For requests like POST, PUT, and DELETE, add data to the request body using the “Body” tab. Choose the appropriate data format (JSON, XML, etc.).

Example: Sending a POST Request with Body Data:

Let’s assume we have an endpoint to create a new user.

  1. Request Method: POST
  2. URL: https://api.example.com/users
  3. Headers:
    • Content-Type: application/json
  4. Body:
    • Raw
    • JSON

Code:

POST https://api.example.com/users
Content-Type: application/json
{
"name": "New User",
"email": "newuser@example.com"
}

6. Setting Up Assertions

  • Tests Tab: Click the “Tests” tab to add assertions to your requests. Assertions allow you to verify specific conditions about the response data.

Example: Assertions for the POST User Request:

pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response body has a name property", function () {
pm.expect(pm.response.json().name).to.be.a('string');
});

These assertions check if the status code is 201 (Created) and if the response body contains a “name” property.

7. Utilizing Collections for Organization

  • Collections: Organize your API requests into groups called Collections. This keeps your tests organized and reusable.

8. Automation and Running Tests

  • Runner: Postman’s built-in Runner lets you run your tests automatically.
  • Integration with CI/CD: Integrate Postman with your Continuous Integration/Continuous Delivery (CI/CD) pipeline (like Jenkins or CircleCI) to automatically run tests as part of your development workflow.

Best Practices

  • Environment Variables: Use environment variables (accessible through the “Environment” tab) to store sensitive data like API keys and URLs.
  • Documentation: Document your tests and API endpoints clearly for maintainability and collaboration.
  • Version Control: Store your Postman collections in version control (e.g., Git) for easier tracking, rollback, and collaboration.

Postman empowers you to test APIs effectively, ensuring your applications function correctly. By utilizing its features and following good practices, you can enhance your testing workflow and release high-quality software.

API Testing Blog