Detailed explanation of the use of Postman's Pre-request Script

Introduction to
Pre-request Script Pre-request Script is a code fragment that needs to be executed before the request is sent in the collection. The request
parameter contains a random number or the request header includes a timestamp, or your request parameter needs to be encrypted.
Add Pre-request Script to the Request
    1. Create a new request, click the Pre-request Script tab, get the Linux timestamp through the js script Math.round(new Date().getTime()/1000), and set the obtained timestamp as an environment variable

    2. Obtain the environment variable value set in the previous step through { {timestamp}} and set it as the request header

 Add the Pre-request Scripts of the Pre-request Script
   collection to the collection (folder) to    act on each request under the collection, and execute the Pre-request Scripts before each request is executed

     1. Select a marriage, click "...", click Edit, the collection editing page will pop up

    2. Select the Pre-request Scripts tab. The interface request of our company is encrypted. I requested an encryption interface in Pre-request Scripts to encrypt the request data, and then set the encrypted data as an environment variable

Commonly used codes in Pre-request Script The
right side provides some common codes
pm.globals.unset("variable_key"); Clear global variables
pm.environment.unset("variable_key"); Clear environment variables
pm.globals.get(" variable_key"); Get global variables
pm.variables.get("variable_key"); Get a variable
pm.environment.get("variable_key"); Get environment variables
pm.sendRequest("https://postman-echo.com/ get", function (err, response) {     console.log(response.json()); }); Send a request pm.globals.set("variable_key", "variable_value"); Set environment variables


 

Guess you like

Origin blog.csdn.net/wuchenlhy/article/details/84316428