Dynamically Modify Query, Body, and Header Parameters Before Sending
In the document "Pre-request Script", we learned that the main functions of pre-request scripts are:
- Writing JS functions to achieve complex calculations;
- Printing variables;
- Defining, retrieving, deleting, and clearing environment variables;
- Defining, retrieving, deleting, and clearing global variables;
- Retrieving request parameters;
- Dynamically adding or deleting a header request parameter;
- Dynamically adding or deleting a query request parameter;
- Dynamically adding or deleting a body request parameter;
- Sending HTTP requests.
Using this feature, we can dynamically add or modify request parameters before sending.
Usage Scenarios
Suitable for situations where request parameters need to be dynamically changed before sending. For example, when sending a request, a token needs to be calculated through other request parameters and added to the corresponding sending parameters.
Script Syntax
pm.setRequestQuery("key", "value"); // Add or modify a query parameter in the URL
pm.removeRequestQuery("key"); // Delete a specified query parameter in the URL
pm.setRequestHeader("key", "value"); // Add or modify a request header key
pm.removeRequestHeader("key"); // Delete a request header key
pm.setRequestBody("key", "value"); // Add or modify a body parameter
pm.removeRequestBody("key"); // Delete a body parameter key
pm.setRequestBody(
{
"key": "value"
}
); // Reset the body parameters "entirely" to {"key": "value"}, suitable for cases where the original request body is of raw type