Skip to content

How Do I Use Postman Files

API Testing Blog

Importing and Using Postman Files

Postman files, with the .postman_collection extension, are powerful tools for storing and sharing API requests. They allow you to organize your tests, share them with colleagues, and efficiently execute complex workflows. Let’s dive into how to effectively utilize Postman files in your API testing journey.

1. Understanding Postman Collection Files

A Postman collection file (.postman_collection) is a JSON-based file that contains a collection of API requests. Imagine it as a library of your API interactions, ready for use. Each collection can contain numerous requests, organized into folders for better management.

2. Importing Postman Files:

You can import Postman files into your Postman environment in a few ways:

a) Direct Import:

  1. In Postman, click the “Import” button located in the top-right corner.
  2. Choose “File” as the import source.
  3. Locate and select your .postman_collection file.
  4. Click “Import” to bring the collection into Postman.

b) Importing from a URL:

  1. Click the “Import” button.
  2. Select “Link” as the import source.
  3. Paste the URL of the .postman_collection file.
  4. Click “Import”.

3. Working with a Postman Collection:

Once you have imported your collection, you can explore its structure.

a) Navigation:

  • Each collection is displayed in the left-hand navigation panel.
  • You can expand folders to see the individual requests within them.

b) Request Details:

  • Click on a request to access its detailed settings.
  • These settings include:
    • HTTP Method (GET, POST, PUT, DELETE etc.)
    • URL
    • Headers
    • Body (params, JSON, form-data etc.)
    • Authorization

c) Executing Requests:

  • Click the “Send” button to execute a specific API request.
  • The response received from the API will be displayed in the Response tab.

4. Example: Using a Postman File for Authentication

Let’s say you need to use an API that requires authentication through an OAuth2 flow. You can use a Postman file to automate this process:

1. Create a Collection:

  • In Postman, click on “Create Collection” and give it a name.
  • Add a folder for OAuth related requests.

2. Create Requests:

  • Within the OAuth folder:
    • Request 1 (Authentication):
      • Method: POST
      • URL: https://api.example.com/oauth/token
      • Headers: Content-Type: application/x-www-form-urlencoded
      • Body: grant_type=client_credentials&client_id=your_client_id&client_secret=your_client_secret
    • Request 2 (API call):
      • Method: GET
      • URL: https://api.example.com/users
      • Headers: Authorization: Bearer {{access_token}}
        • {{access_token}} is a variable. We’ll configure it later.

3. Variables:

  • In the collection’s settings, create a new variable named access_token.

4. Pre-request Script:

  • For the “API call” request, add a pre-request script:
pm.environment.set('access_token', pm.response.json().access_token);

This script will extract the access token from the authentication response and store it in the access_token variable.

5. Running the Collection:

  • First, run the authentication request.
  • Then, run the API call request. The access_token variable will be dynamically used in the authorization header.
  • Finally, save this collection as a .postman_collection file.

5. Sample Code - Postman Collection File

Here is a basic example of a Postman collection file structure:

{
"info": {
"_postman_id": "d7617559-7324-4854-a320-e74c88ba5d1b",
"name": "My First Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Users",
"item": [
{
"name": "Get User",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": {
"raw": "https://api.example.com/users",
"host": [
"api",
"example",
"com"
],
"path": [
"users"
]
}
},
"response": []
},
{
"name": "Create User",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": {
"raw": "https://api.example.com/users",
"host": [
"api",
"example",
"com"
],
"path": [
"users"
]
},
"body": {
"mode": "raw",
"raw": "{ \"name\": \"John Doe\", \"email\": \"john.doe@example.com\" }"
}
},
"response": []
}
]
}
]
}

This example shows a collection named “My First Collection” with a “Users” folder containing two requests: “Get User” and “Create User”. Each request is defined with its method, headers, URL, and body as needed.

6. Sharing and Collaboration:

Postman files facilitate collaboration and knowledge sharing.

a) Exporting Collection:

  • In the collection’s settings, click “Export”.
  • Choose the desired format (Postman Collection v2.1.0, Postman Collection v2.0.0, or raw JSON).
  • Download or share the file as needed.

b) Workspace Sharing (Postman Pro):

  • Postman Pro users can share collections directly within their workspaces.
  • This allows team members to access, edit, and contribute to shared collections.

By leveraging Postman files, you streamline your API testing process, enhance collaboration, and foster a more efficient workflow for your software development team.

API Testing Blog