Skip to content

How To Use Postman To Test An Api

API Testing Blog

Getting Started with Postman for API Testing

Postman is a powerful tool for API testing, allowing you to send requests, inspect responses, and automate your tests. This guide will walk you through the fundamentals of using Postman for API testing.

1. Creating a Request

Start by opening Postman and creating a new request. You can do this by clicking the “New” button and selecting “Request”. In the request builder, you’ll see several sections:

a. Request Method: Select the HTTP method for your request (e.g., GET, POST, PUT, DELETE).

b. URL: Enter the API endpoint you want to test.

c. Headers: Add any necessary headers, such as authorization tokens or content type information.

d. Body: This is where you’ll define the request body, if your API requires one.

e. Sending the Request

Once you’ve configured your request, click the “Send” button to execute it.

2. Inspecting the Response

After sending a request, Postman displays the response from the API. You can review several aspects:

a. Status Code: The HTTP status code indicates whether the request was successful (e.g., 200 for OK) or encountered an error (e.g., 404 for Not Found).

b. Response Headers: Examine the headers returned by the API.

c. Response Body: The response body contains the data returned by the API. Postman provides various ways to view the body, including raw text, JSON, XML, and HTML.

3. Using the Postman Console

The Postman Console is a valuable tool for debugging your API interactions. It displays detailed information about your requests and responses, including:

a. Request and Response Details: See all the aspects of your request and the corresponding response.

b. Error Messages: Identify any errors that might occur during the request.

c. Execution Time: Monitor how long it takes for the API to respond.

4. Creating Tests

Postman allows you to write automated tests to validate your API’s functionality. This is done using a scripting language called Postman’s Test Scripts

a. Adding Test Scripts

Click the “Tests” tab in the request builder to access the test script editor. You can use JavaScript to write your tests.

b. Test Example - Basic Validation

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

In this example, the first test checks if the status code is 200, while the second test verifies if the response body contains the string “success”.

5. Organizing Your Tests with Collections

Collections in Postman allow you to group your API tests into logical units. This helps with organization and reusability.

a. Creating a Collection:

Click the “Collections” button and select “Create Collection”.

b. Adding Requests to a Collection:

Drag and drop requests from the “Requests” section into your newly created collection.

c. Organizing with Folders:

You can further organize your collection by creating folders within it to separate tests conceptually.

6. Running Tests with the Collection Runner

Postman’s Collection Runner enables you to execute all the requests in a collection in a sequential order.

a. Accessing the Collection Runner:

Click the “Runner” button, select your collection, and configure any desired settings (like environment variables).

b. Running Tests:

Click the “Run” button to execute the tests in your collection.

7. Using Environments for Configuration

Postman Environments allow you to manage different configurations for your tests (e.g., using different base URLs for development and production).

a. Creating an Environment:

Click the “Environments” button and select “Create Environment”.

b. Setting Variables:

Add variables within your environment (e.g., “baseUrl”, “apiKey”).

c. Using Variables in Requests:

In your requests, access environment variables using double curly braces (e.g., {{baseUrl}}).

Example - Switching Environments:

For example, here’s how to define a base URL for your development environment:

Environment:

Name: Dev
Variable: baseUrl
Value: http://localhost:3000

Request:

GET {{baseUrl}}/users

By switching to the “Dev” environment, Postman will use “http://localhost:3000” as the base URL for this particular request.

API Testing Blog