0
0
Postmantesting~8 mins

Request headers in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Request headers
Folder Structure for Postman Test Collections
Postman-Tests/
├── collections/
│   ├── User-API.postman_collection.json
│   ├── Product-API.postman_collection.json
│   └── Auth-API.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── globals/
│   └── globals.postman_globals.json
├── scripts/
│   ├── pre-request-scripts.js
│   └── test-scripts.js
└── README.md
Test Framework Layers in Postman
  • Collections: Group of API requests organized by feature or service. Each request can have its own headers.
  • Environments: Variables for different setups (e.g., dev, staging) including base URLs and tokens.
  • Globals: Variables accessible across all collections and environments.
  • Pre-request Scripts: JavaScript code run before each request to set or modify headers dynamically.
  • Tests: JavaScript assertions run after response to validate results.
Configuration Patterns for Request Headers
  • Environment Variables: Store common header values like Authorization tokens or Content-Type to reuse across requests.
  • Global Variables: Use for headers that apply to all requests regardless of environment.
  • Pre-request Scripts: Dynamically set or update headers before sending requests, e.g., generate fresh tokens.
  • Collection-level Headers: Define headers that apply to all requests in a collection to avoid repetition.
  • Secure Storage: Keep sensitive header values like API keys in environment variables, not hardcoded in requests.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line with detailed reports in JSON, HTML, or JUnit formats.
  • CI/CD Pipelines: Integrate Newman runs in pipelines (e.g., Jenkins, GitHub Actions) to automate API tests on code changes.
  • Report Analysis: Use generated reports to track header-related test failures like missing or incorrect headers.
  • Notifications: Configure alerts on test failures to quickly fix header or authentication issues.
Best Practices for Request Headers in Postman Framework
  • Reuse Headers via Variables: Use environment or global variables for headers to avoid duplication and ease updates.
  • Secure Sensitive Data: Never hardcode secrets in headers; use environment variables with restricted access.
  • Use Pre-request Scripts: Automate header updates like token refresh to keep tests reliable.
  • Organize Collections by Feature: Group requests logically so headers relevant to a feature are managed together.
  • Validate Headers in Tests: Write assertions to check if required headers were sent or received correctly.
Self Check Question

Where in this Postman framework structure would you add a new header for API authentication that changes per environment?

Answer: In the environments/ folder, add or update the environment variable (e.g., auth_token) and then reference it in the request headers or pre-request scripts.

Key Result
Organize Postman tests with collections, environments, and scripts to manage request headers efficiently and securely.