How to send a request in ApiPost pre-execution script

Posted May 26, 20203 min read

In the document ApiPost's pre-execution script and post-execution script , we understand that the role of pre-execution script is mainly:

The pre-execution script is a script that is executed before the current interface sends a request, which can be understood to be executed when beforeSend. Generally, here, we can set some variables needed for sending in advance.

This article mainly introduces how to send a request in advance in the pre-execution script.

scenes to be used

We can first send a request in the pre-execution script and bind the request result to a variable, and then use this variable in the request parameters of the current interface.

  1. Send a simple GET request

You can implement a simple request in the ApiPost client through the following script.

Send a request in ApiPost pre-execution script

As shown:

Send a request in ApiPost pre-execution script

The above script is a very basic request script with the following meanings:

To http://echo.apipost.cn/token.... , and assign the token of the response data(json) to the environment variable var.

In this case, the current interface can use the variable var in the request parameters. As shown:

Send a request in ApiPost pre-execution script

  1. Send a more complex request

The above example shows how to send a simple get request in a pre-script and assign the response value to a variable. Next, explain how to send a more complex request, such as with parameters.

In fact, for apipost's apt.sendRequest method, its usage is similar to ajax in jquery, and the parameters are almost the same as ajax. The structure is as follows:

apt.sendRequest(option, callback)

option:

This parameter can be a URL or an object. When it is a URL(as in the example above), it sends a simple GET request. When it is an object, the object parameters and meanings are as follows:

Send a request in ApiPost pre-execution script

Note:The meaning of the above parameters is the same as the meaning of the jQuery $.ajax parameter.

callback:

This is the response callback function, similar to the success callback function in ajax.

Sending example:

  1. Send a request with content-type appicateion-json

Send a request in ApiPost pre-execution script

The meaning of the above example:

Request url:

https://echo.apipost.cn/token ...

Request method:

post

Request content-type:

appicateion-json

Request parameters:

Send a request in ApiPost pre-execution script

Note:When content-type:appicateion-json, request json object parameters need to be processed into json string by JSON.stringify before sending.

Callback: The res parameter of the callback function is the response result. Here, the token of the response structure is assigned to the variable var.

  1. Send a request with content-type x-www-form-urlencoded

Send a request in ApiPost pre-execution script

The meaning of the above example:

Request url:

https://echo.apipost.cn/token...

Request method:

post

Request content-type:

application/x-www-form-urlencoded

Request parameters:

Send a request in ApiPost pre-execution script

Callback:

The res parameter of the callback function is the response result. Here, the token of the response structure is assigned to the variable var.

Other requests are the same, so I wo n t go into details here.