In a real production environment, we often encounter the problem that one interface often requires the return value of another interface as a request parameter.

For example, we need to obtain the token through one interface A and then send the token as the request parameter of the second interface B.

This article will solve this problem.

To facilitate the demonstration, we prepare two interfaces first: interface A to obtain the token, and interface B to use the token.

1. Interface A for obtaining the token

Interface URL: echo. Apipost. Cn/token. PHP

content-type: application/json,

Request the Body argument:

{
  "moible": 1388888666,"password":"123456"
}Copy the code

Return example:

{
  "errcode": 0."errstr":"success"."token":"63fabf20700e17ac34d7f90d6d03caae"
}Copy the code

2. Token interface B

Interface URL: echo.apipost.cn/echo.php

content-type: x-www-form-urlencoded,

Request the body argument:

{
  "token":? // We need to get the token from the token interface as the request parameter}Copy the code

Return example:

{
    "errcode": 0."errstr": "success"."post": [// submitted request body parameter],"header": {
        "Host": "echo.apipost.cn"."Connection": "keep-alive"."Upgrade-Insecure-Requests": "1"."User-Agent": "\ Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/78.0.3904.108 Safari\/537.36"."Accept": "text\/html,application\/xhtml+xml,application\/xml; Q = 0.9, image \ / webp image \ / apng, * \ / *; Q = 0.8, application \ / signed - exchange; v=b3"."Accept-Encoding": "gzip, deflate"."Accept-Language": "zh-CN,zh; Q = 0.9"."Cookie": "UM_distinctid=1709ee7b93f4-069ba9e2aa711c-2393f61-13c680-1709ee7b940389; PHPSESSID=oumoolom1338i8aafc6p3a1mhn; BAIDU_SSP_lcr=https:\/\/blog.csdn.net\/weixin_45316122\/article\/details\/95252977; Hm_lvt_a046ce178828e393614822a297b8d296 = 1588239504158239, 641158239, 650158252, 498; Hm_lpvt_a046ce178828e393614822a297b8d296=1588253907"}}Copy the code

3. Start implementing

To prepare

Open the APIPost, create an interface, and fill in the URL of interface B: echo.apipost.cn/echo.php

In addition, we define a variable {{token_var}} to put in the request body parameter, as shown in the figure below

Because {{token_var}} is not assigned, the server output {{token_var}} is not what we want.

We then assign the variable in two ways.

Implementation method 1:

Create an interface, and fill in the information of interface A with request parameters and URL, as shown below:

Then click “Execute Script after” and enter the following script:

apt.variables.set("token_var", response.json.token);Copy the code

What this script means is that the token that responds to JSON is assigned to the variable token_var

After sending, we go to interface B again and see that the server has successfully received the token, as shown in the following figure:

Implementation method two:

Let’s go to the “preexecute script” option of interface B, select [send a request], and change the sample request to the following script:

apt.sendRequest({
    "method":"post"."url":"https://echo.apipost.cn/token.php"."content-type":"application/json"."data":JSON.stringify({
            	"mobile": 1388888666,
            	"password": "123456"})},function (response) {
	apt.variables.set("token_var", response.token);
});
Copy the code

The meaning of this script is to echo. The apipost. Cn/token. PHP sends a content-type post request for application/json, and returns the result of the token is assigned to variables: token_var

As shown below:

At this point, we click Send again and see that the server has successfully received the token, as shown below:

Reference Documents:

Pre – and post-execution scripts for the ApiPost

How does ApiPost V3 set a variable?