Skip to content

How To Check Api Using Postman

API Testing Blog

A Comprehensive Guide on API Testing with Postman

Postman is a powerful tool for testing APIs. It offers a user-friendly interface, allows for easy creation and execution of requests, and facilitates the analysis of responses. This guide will walk you through the process of using Postman for API testing, covering various aspects from basic requests to advanced features.

Sending Your First API Request: A Simple GET Example

  1. Install Postman: Begin by downloading and installing Postman from https://www.postman.com/.
  2. Create a Request: Open Postman and click on the “New” button to create a new request.
  3. Choose the HTTP Method: Select the appropriate HTTP method (GET, POST, PUT, DELETE, etc.) for your API interaction.
  4. Enter the API Endpoint URL: In the address bar, enter the URL of the API you want to interact with.
  5. Send the Request: Click the “Send” button to send the request to the API.

Example: To get the latest weather information for London from OpenWeatherMap:

  • Method: GET
  • Endpoint URL: https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY (Replace YOUR_API_KEY with your actual OpenWeatherMap API key)

Result: You will see the API response in the “Body” tab of Postman, containing information about the weather in London.

Understanding Request Parameters

API interactions often require passing data through parameters to influence the returned information.

Param Types:

  • Query Parameters: Added to the URL after the endpoint using a question mark and key-value pairs (e.g., ?key1=value1&key2=value2).
  • Headers: Provide metadata about the request. They are typically used for authentication, setting content types, or specifying desired response formats.
  • Body: Used to send data to the server, typically used with methods like POST and PUT for creating or updating resources.

Adding Parameters in Postman:

  • Query Parameters: Add parameters in the “Params” tab of Postman.
  • Headers: Add headers in the “Headers” tab.
  • Body: Choose the appropriate format (raw, form-data, JSON) in the “Body” tab.

Example: Fetching data from a user specific to a user ID:

  • Method: GET
  • Endpoint: https://api.example.com/users
  • Query Parameter: ‘userId=123’
  • Result: The API will return data associated with ‘userId=123’.

Verifying API Responses

Postman offers several features to analyze API responses, ensuring the API behaves as expected.

Response Validation:

  • Status Codes: Check for the correct HTTP status code in the response. For example, a 200 status code indicates a successful request.
  • Headers: Review the response headers to verify the data format (e.g., content-type) or potential errors.
  • Body: Examine the content of the response body. Postman allows you to view the response in different formats (JSON, XML, text etc.)

Example (JSON): Validating the weather API response:

  • Expected: The response should be JSON and include fields such as ‘main’, ‘temp’, and ‘description’ within the JSON structure.
  • Verification: Use Postman’s “Test” tab to write code (using JavaScript) to check for the presence of these fields in the JSON response.

Adding Tests for Greater Control:

For more rigorous testing scenarios, Postman allows you to define and run tests against your API.

Creating Tests:

  1. Test Tab: Navigate to the “Test” tab in Postman.
  2. JavaScript Assertions: Write Javascript code within the tab to define test cases. You can use built-in Javascript assertions like pm.test() and pm.expect().

Example: Testing the OpenWeatherMap API for a specific temperature:

pm.test("Temperature is above 20 degrees Celsius", () => {
pm.expect(pm.response.json().main.temp).to.be.above(20);
});

Working with Environments

Postman allows you to store different configurations, such as API keys, base URLs, or other variables, in environments. This feature is useful for testing scenarios that require switching between different configurations.

Creating an Environment:

  1. Environments tab: Navigate to the “Environments” tab.
  2. Add an Environment: Click on the “Add” button to create a new environment.
  3. Add Variables: Define variables within the environment.

Example:

  • Environment: ‘Production’

  • Variable: apiKey

  • Value: YOUR_PRODUCTION_API_KEY

  • Environment: ‘Staging’

  • Variable: apiKey

  • Value: YOUR_STAGING_API_KEY

You can then select the appropriate environment to use for different tests.

Automating Tests with Collections

Postman collections allow you to group several related requests into a logical unit. Collections are valuable for organizing and automating test suites.

Creating a Collection:

  1. Collections Tab: Navigate to the “Collections” tab.
  2. Add a Collection: Click on the “Add” button or the “Create new collection”.
  3. Add Requests: Add individual requests you have created to the collection.

Running Collections:

  1. Collection Runner: Click on the “Runner” button to test the collection.
  2. Environment Selection: Choose the environment to use for the tests.
  3. Iterations: Set the number of times you want to run the test suite.
  4. Data: Optionally, add data to be used as parameters for the requests.

Example: Testing the OpenWeatherMap API for Weather in different cities:

  1. Collection: “Weather Tests”
  2. Requests:
    • “London Weather”
    • “Paris Weather”
    • “Tokyo Weather”
  3. Run the Collection: You can now run the entire collection to test the API for multiple locations.

Postman for Collaborative Testing

Postman is also an excellent tool for collaborative testing efforts:

  • Sharing Collections: Share collections with team members for easy collaboration.
  • Shared Environment: Use shared environments to store common credentials and variables.
  • Postman Workspace: Collaborate in a shared workspace to organize, share, and run tests as a team.

Conclusion

Postman is an invaluable tool for testing APIs, offering a comprehensive set of features. Whether you are testing a simple API request or building an automated test suite, Postman provides a versatile and user-friendly platform. By mastering the techniques presented in this guide, you can effectively test your APIs and ensure they deliver reliable and expected functionality. The examples provided throughout will help get you started on your journey to becoming a Postman pro.

API Testing Blog