Pass data between requests
In automated testing scenarios, passing data between multiple requests is very common. Typical cases include:
- Request 1 is a login request that returns a token; Request 2 uses this token to request other data.
- Request 1 returns an ID; Request 2 performs operations based on this ID.
- Request 1 returns a list; Request 2 uses data from this list.
For such scenarios, Apidog provides two different solutions to handle data passing between requests. You can choose the appropriate solution based on the specific problem at hand.
- Retrieve pre-step data: This feature allows you to directly use data from preceding requests in subsequent requests.
- The advantage is simplicity, without the need to introduce additional variables.
- However, it may become slightly cumbersome when data needs to be referenced multiple times.
- Retrieve pre-step data feature can be ONLY used in the "Tests" module and not in the "APIs" module.
- Use variables: Add "Extract Variables" in the pre-processor of the preceding request to store the required data in a variable, and then call this variable in the subsequent requests.
- The advantage is convenience when referencing data multiple times, but it may be slightly more complex for single references.
- Variables can be used both in "Tests" module and "APIs" module.
Retrieve pre-step data
Let's consider a scenario where Request 1 is a login request that returns a token, and Request 2 uses this token to request additional data.
</Step>
<Step>
Click Insert, and you'll see {{$.2.response.body.token}}
inserted as the query param.
</Step>
<Step>
Click the "Run" button in the Test scenario to successfully pass the data from Request 1 to Request 2.
</Step>
:::tip[]
- The feature of "Retrieve pre-step data" is ONLY AVAILABLE in the "Tests" module and not in the "APIs" module.
- When using "Retrieve pre-step data", the value can ONLY be obtained when the entire test scenario is run together; it cannot be accessed when running individual steps.
:::
Referencing pre-step data using variable syntax
Using the example of {{$.2.response.body.token}}
from the previous text:
- '2' represents the step ID, which can be found in each test step.
- 'response.body' indicates the position of the data from the pre-step. This can include data from the request's header, body, or the response's header or body, among others. See below for specifics.
- 'token' represents the 'token' data at the next level within the body. You can use JSONPath syntax to extract the desired data.
Pre-step data can be utilized in various parts of the request, such as request parameters, headers, authentication, and more. You can also directly insert the data into the request body, as shown below.
It's important to note that if you need to use pre-step data in your scripts, you cannot directly reference variables using {{variable}}
syntax. Instead, you should use pm.variables.get
to reference pre-step data. For example:
var token = pm.variables.get("$.2.response.body.token")
Syntax reference
Category | Function | Syntax Example |
---|---|---|
Request | URL | {{$.<step id>.request.url}} |
Path parameter | {{$.<step id>.request.pathParam.<field name>}} | |
Query | {{$.<step id>.request.query.<field name>}} | |
Header | {{$.<step id>.request.header.<field name>}} | |
Body (form) | {{$.<step id>.request.body.<field name>}} | |
Body (json) | {{$.<step id>.request.body.<field path>}} | |
Response | Body | {{$.<step id>.response.body.<field path>}} |
Header | {{$.<step id>.response.header.<field name>}} | |
Cookie | {{$.<step id>.response.cookie.<field name>}} | |
Loop | Element (array element in ForEach loop) | {{$.<loop step id>.element.<field path>}} |
Index | {{$.<loop step id>.index}} |
Use variables to pass data
Let's consider a scenario where Request 1 is a login request that returns a token, and Request 2 uses this token to request additional data.
:::highlight purple
Learn more about Extract variable.
:::
Add the query request (Request 2) to the test scenario.
</Step>
<Step>
Click the "Run" button in the Test scenario to successfully pass the data from Request 1 to Request 2.
</Step>
FAQ
Q: Why am I not able to successfully reference pre-step data?
A: Firstly, make sure you're now in "Tests" module. "Referencing pre-step data" feature is available only in Tests module but not APIs module.
If you're now surely in Tests module, switch to the "Actual Request" tab to confirm whether the pre-step data referenced in the request has been successfully incorporated.
If the reference in the actual request still appears as {{$.n.response.body.abc}}
instead of showing actual data, it indicates that the reference has not been applied.
In this case, consider the following common reasons:
- Have you run the entire test scenario instead of just a single step? Referencing pre-step data requires running the complete test scenario for it to take effect.
- Verify if the step ID is correct and corresponds to the step you intend to reference.
- Ensure that the JSONPath used matches the data source accurately.