Skip to content

How To Use Postman Api

API Testing Blog

A Comprehensive Guide to Using Postman for API Testing

Postman is a powerful tool used for building, testing, and documenting APIs. It’s a popular choice among developers and testers due to its user-friendly interface and robust features. In this guide, we’ll cover the basics of using Postman for API testing, exploring practical examples and step-by-step instructions.

Understanding the Basics - How to use Postman API for the First Time

Before diving into testing, let’s understand the fundamental components of a Postman request:

  • Method: The HTTP method (GET, POST, PUT, DELETE, etc.) used to interact with the API.
  • URL: The endpoint URL of the API you want to test.
  • Headers: Key-value pairs used to provide additional information about the request.
  • Body: The data you send to the API, often in JSON format.
  • Authorization: How your request is authenticated.

Setting up a Request in Postman

  1. Launch Postman: Open the Postman application.

  2. Create a new request: Click on the “New” button and select “Request.”

  3. Choose the method: Select the appropriate HTTP method for your request. For example, if you’re requesting data from an API, you’ll likely use the “GET” method.

  4. Enter the URL: Fill in the URL of the API endpoint you want to test.

  5. Add Headers (if needed): If the API requires specific headers, add them to the “Headers” tab. For instance, if you need to include an “Accept” header to specify the format of the response, you can add it here.

  6. Add Body (if needed): If your request requires sending data to the API, provide it in the “Body” tab. Select the appropriate format (JSON, form data, etc.) and populate the fields.

Sending the Request

  1. Click on the “Send” button to execute your request.

  2. Examine the results: Postman will display the response from the API in the “Body” tab. This includes the status code, headers, and response data.

How to Use Postman API to Test GET Requests

Here’s a practical example of sending a GET request using the famous “JSONPlaceholder” API:

1. Open Postman and create a new request.

2. Select “GET” as the method and enter the URL: https://jsonplaceholder.typicode.com/users

3. Click “Send”.

You’ll receive a response with a status code of 200 (success) and a JSON array containing user data.


How to Use Postman API to Test POST Requests

Let’s create a new user using the same JSONPlaceholder API:

1. Create a new request and set the method to “POST”.

2. Enter the URL: https://jsonplaceholder.typicode.com/users

3. In the Body tab, select “raw” and choose “JSON” as the format.

4. Paste the following JSON data:

{
"name": "John Doe",
"username": "johndoe",
"email": "john.doe@example.com"
}

5. Click “Send”.

You’ll receive a confirmation with a status code of 201 (Created) and the newly created user details.


How to Use Postman API to Test PUT Requests

To update an existing user:

1. Create a new request and set the method to “PUT”.

2. Enter the URL: https://jsonplaceholder.typicode.com/users/1 (replace 1 with the user ID you wish to update).

3. In the Body tab, select “raw” and choose “JSON” as the format.

4. Paste the following JSON data:

{
"name": "Jane Doe",
"username": "janedoe"
}

5. Click “Send”.

You will get a response with a status code of 200 (Success) and the updated user information.


How to Use Postman API to Test DELETE Requests

To delete a user from the JSONPlaceholder API:

1. Create a new request and set the method to “DELETE”.

2. Enter the URL: https://jsonplaceholder.typicode.com/users/1 (replace 1 with the user ID you want to delete).

3. Click “Send”.

You’ll get a response with a status code of 200 (Success) indicating that the user has been successfully deleted.


How to Use Postman API to Work with Collections

Postman Collections organize your requests and make it easier to test and manage your API workflows.

1. Create a new Collection: Click on the ”+” icon and select “Collection.” Give your collection a name.

2. Add requests to the collection: Drag and drop requests into the collection, or create new requests directly within the collection.

3. Organize requests within folders: You can group related requests into folders within the collection for better organization.

4. Run collections: You can run the requests in a collection sequentially using the “Run” button.

5. Environment Variables: Collections can leverage environment variables to manage different API environments (e.g., development, testing, production).

How to Use Postman API with Environment Variables

Environment variables enable you to separate different configurations (like base URLs or API Keys) for various environments.

1. Create an Environment: Click on the ”+” icon and select “Environment.” Give your environment a name.

2. Add Variables: In the environment editor, add key-value pairs representing your environment variables.

3. Use Variables in Requests: Use the syntax {{variable_name}} within your request URLs, headers, or body to insert environment variable values.

For example, let’s define an environment variable for the base URL called “baseUrl”:

baseUrl: https://api.example.com

In your request URL, instead of hardcoding the full URL, you can use: {{baseUrl}}/users.

How to Use Postman API to Test with Assertions

Assertions in Postman allow you to verify if an API response meets your expected criteria.

  1. Add an assertion: To add an assertion, click on the “Test” tab in your request.

  2. Write the assertion: Postman provides a variety of assertion types (e.g., “status code” to check the HTTP status code, “response body” to validate the content, etc.). You can write custom JavaScript code to define your own assertions.

Here’s an example of asserting the status code of a GET request:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

How to Use Postman API for Continuous Integration

Postman can be integrated with CI/CD tools to automate your API testing process.

  1. Postman Newman: Newman is a command-line tool for running Postman collections. You can use Newman in your CI/CD pipelines to execute tests and report results.

  2. Postman CLI: The Postman CLI helps you manage collections, environments, and more from your terminal.

Integrating with CI/CD Pipelines

  1. Install Newman: Use npm (Node Package Manager) to install Newman: npm install -g newman

  2. Create a Newman script: Write a script to run your Postman collection using Newman.

Example Newman script:

#!/bin/bash
newman run "path/to/your/collection.json" \
-e "path/to/your/environment.json" \
-r json > results.json
  1. Include the script in your CI/CD pipeline: Integrate the Newman script into your CI/CD pipeline (e.g., Jenkins, GitLab CI/CD, CircleCI) to execute the tests automatically on each build.

Conclusion

Postman is a versatile and valuable tool for API testing. Its user-friendly interface, robust features, and integration with CI/CD pipelines make it a popular choice for developers and testers. By mastering the basics of Postman, you can write comprehensive tests, improve API quality, and release better software faster.

API Testing Blog