Skip to content

How To Use Postman For Api Calls

API Testing Blog

Introduction to Postman

Postman is a powerful tool for interacting and testing APIs. It offers a user-friendly interface and a range of features for making requests, viewing responses, and automating tests. This guide will walk you through the basics of using Postman for API calls.

Setting Up Postman

  1. Download and Install Postman: Visit the official Postman website (https://www.postman.com/) and download the app for your operating system.
  2. Create an Account: You can create a free Postman account to access additional features and syncing capabilities.
  3. Open the Postman App: Launch Postman to start working with API requests.

Making Your First API Call

  1. Open a New Request: Click the “New” button in the top left corner to create a new request.
  2. Select the HTTP Method: Choose the appropriate HTTP method (GET, POST, PUT, DELETE, etc.) from the dropdown menu.
  3. Enter the API URL: Paste the URL of the API endpoint you want to interact with in the request bar.
  4. Add Headers (Optional): If the API requires authentication or other headers, click the “Headers” tab and add the necessary key-value pairs.
  5. Add Body (Optional): For POST, PUT, and PATCH requests, you can add data to the request body. Click the “Body” tab and select the appropriate data format (form data, JSON, raw, etc.).
  6. Send the Request: Click the “Send” button to execute the API call.

Example: Getting Data from a Weather API

Request:

GET https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY

Headers:

  • Content-Type: application/json

Body: (Empty for GET request)

Response:

{
"coord": {
"lon": -0.1257,
"lat": 51.5074
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 280.15,
"temp_min": 280.15,
"temp_max": 284.15,
"pressure": 1015,
"humidity": 81
},
"visibility": 10000,
"wind": {
"speed": 4.6,
"deg": 200
},
"clouds": {
"all": 0
},
"dt": 1687814099,
"sys": {
"type": 2,
"id": 2000218,
"country": "GB",
"sunrise": 1687777268,
"sunset": 1687824143
},
"timezone": 3600,
"id": 2643743,
"name": "London",
"cod": 200
}

Working with Request Collections

Postman allows you to organize your API requests into collections. This is valuable for testing complex workflows and API interactions.

  1. Create a New Collection: Click the “Collections” button in the left sidebar and create a new collection.
  2. Add Requests to the Collection: Add existing requests or create new requests within the collection.
  3. Run Collections: You can run your collection as a single test suite. Postman will execute each request in order.

Example: User Registration and Login Collection

Collection Structure:

  • Register User: POST request to /users/register
  • Login User: POST request to /users/login

Postman Runner can be used to execute all requests in the collection to test the entire user registration and login workflow.

Handling Authentication

Many APIs require authentication to access protected resources. Postman offers several authentication methods:

  1. Basic Auth: Provide username and password directly in the request headers.
  2. Bearer Token: Send an access token in the Authorization header.
  3. API Keys: Pass an API key as a parameter in the request URL or header.
  4. OAuth 2.0: Use OAuth 2.0 flow for authorization.

Example: Using Bearer Token for Authentication

Authorization Tab:

  • Type: Bearer Token
  • Token: YOUR_ACCESS_TOKEN

Request Header:

  • Authorization: Bearer YOUR_ACCESS_TOKEN

Testing API Responses

Postman includes built-in mechanisms for testing API responses:

  1. Assertions: Use “Tests” tab to write JavaScript code to assert various aspects of the response (status code, body content, headers).
  2. Pre-request Scripts: Run JavaScript code before sending the request to modify request parameters, set environment variables, etc.
  3. Post-request Scripts: Execute JavaScript code after receiving the response to perform actions like data extraction, data manipulation, reporting, etc.

Example: Asserting Response Status Code:

“Tests” tab:

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

Automating API Tests

Postman allows you to automate your API tests for continuous integration and regression testing:

  1. Create Test Suites: Organize tests into suites for better management.
  2. Run Tests in the Runner: Use Postman Runner to execute test suites and generate reports.
  3. Integrate with CI/CD: Connect Postman to your CI/CD pipeline to automatically run tests with each code build.

Example: Creating a Test Suite for User Login:

Test Suite:

  • User Login Test: Assert successful login with valid credentials.
  • Invalid Credentials Test: Assert error response for invalid credentials.

Conclusion

Postman is a versatile tool for making API calls, testing API responses, and automating API workflows. This guide provides a comprehensive overview of the basic features and functionalities of Postman. By understanding these concepts and practicing with practical examples, you can effectively utilize Postman for your API testing and development needs.

API Testing Blog