Use Pre-request Script to Send a Request
In the document "Pre-execution Scripts", we learned that the main functions of pre-execution scripts are:
- Writing JS functions to achieve complex calculations;
- Printing variables;
- Defining, obtaining, deleting, and clearing environment variables;
- Defining, obtaining, deleting, and clearing global variables;
- Obtaining 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.
This article mainly introduces how to send a request in a pre-execution script.
Usage Scenario
We can send a request in the pre-execution script first, bind the request result to a variable, and then use this variable in the request parameters of the current interface.
Method to Send Requests: $.ajax
$.ajax is an asynchronous method, which may cause the pre-execution script to execute the $.ajax method only after the request is completed.
Supports converting asynchronous methods to synchronous methods using await to ensure that this function is executed first before sending the interface.
Request Example
1. Sending a simple GET request
You can implement a simple request through the following script in the EchoAPI client.
In the EchoAPI pre-request script, send a request:
// Asynchronous call
$.ajax({
method: "GET",
url: "https://httpbin.org/anything",
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
// Synchronous call
await $.ajax({
method: "GET",
url: "https://httpbin.org/anything",
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
The above script is a very basic request script, meaning:
Send a GET request to the specified URL and assign the bigint from the response data (json) to the environment variable origin.
In this way, the current interface can use the variable bigint in the request parameters. As shown in the figure:
2. Sending a request with content-type as application-json
// Asynchronous call
$.ajax({
url: "https://httpbin.org/anything",
"content-type": "application-json",
data: JSON.stringify({"email": "admin@admin.com", "password": "password"}),
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
// Synchronous call
await $.ajax({
url: "https://httpbin.org/anything",
"content-type": "application-json",
data: JSON.stringify({"email": "admin@admin.com", "password": "password"}),
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
Note: When content-type is application-json, the request json object parameters need to be processed into json strings using JSON.stringify before sending.
3. Sending a request with content-type as x-www-form-urlencoded
// Asynchronous call
$.ajax({
url: "https://httpbin.org/anything",
async: false,
"content-type": "application/x-www-form-urlencoded",
data: {"email": "admin@admin.com", "password": "password"},
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
// Synchronous call
await $.ajax({
url: "https://httpbin.org/anything",
async: false,
"content-type": "application/x-www-form-urlencoded",
data: {"email": "admin@admin.com", "password": "password"},
success: function(response) {
console.log(response);
pm.environment.set("origin", response.origin);
}
})
Other request methods are similar and will not be repeated here.