In this tutorial, we will explore the scripting language used in Postman. Postman is a powerful API testing and development tool that allows users to send requests to APIs and receive responses. With its scripting capabilities, users can automate tasks, create complex workflows, and perform advanced operations.
Scripting in Postman
Scripting in Postman allows users to extend the functionality of their API tests and collections. With scripts, you can:
- Create dynamic variables: Use script statements to generate random values or compute values based on existing data.
- Modify request headers: Add or remove headers before sending a request.
- Extract data from responses: Parse JSON or XML responses to retrieve specific data for further processing.
- Create assertions: Verify response status codes, content types, or specific values within the response body.
- Create custom workflows: Define complex workflows by chaining requests together and using control flow statements like loops and conditionals.
The Pre-request Script
The pre-request script is executed before sending a request. It allows you to dynamically modify the request before it is sent. This can be useful for setting authentication headers, generating tokens, or adding dynamic parameters.
The Test Script
The test script is executed after receiving a response. It allows you to extract data from the response, perform assertions, and set environment variables based on the response data.
The Postman Sandbox also includes built-in libraries that can be used within scripts. These libraries include:
- pm: The Postman object model that provides access to request and response details.
- pm.environment: Allows you to read and write environment variables.globals: Allows you to read and write global variables.collectionVariables: Provides access to collection variables.
- _lodash: A utility library that provides useful functions for manipulating data.