REST API variable chaining¶
REST API variable chaining is a technique used in API testing and development workflows to extract values from one HTTP response and inject them into subsequent requests.^[600-developer-tools-vscode-restclient-postman.md]
This method is primarily utilized to manage stateful interactions, such as passing authentication tokens or session IDs returned from a login endpoint to other API calls that require authorization.^[600-developer-tools-vscode-restclient-postman.md]
Implementation in Tools¶
VSCode REST Client¶
In the VSCode REST Client extension, chaining is handled via a custom request syntax that captures response data into a variable^[600-developer-tools-vscode-restclient-postman.md#L37-50]. To extract a value, a request must be assigned a name using the # @name comment. Variables are then defined using the syntax @variableName = {{requestName.response.body.$.jsonPath}}^[600-developer-tools-vscode-restclient-postman.md].
For example, to extract a session ID from a login response:
# @name loginCWS
POST {{domain}}/sessions HTTP/1.1
...
@authToken = {{loginCWS.response.body.$.id}}
authToken variable can then be reused in headers for later requests^[600-developer-tools-vscode-restclient-postman.md].
Postman¶
Postman achieves variable chaining through test scripts written in JavaScript that execute after a request completes^[600-developer-tools-vscode-restclient-postman.md#L55-73]. The script parses the responseBody, extracts specific fields, and assigns them to environment variables using pm.environment.set(variableName, value)^[600-developer-tools-vscode-restclient-postman.md].
A typical script involves parsing the JSON response, logging specific values to the console for debugging, and setting them for subsequent use^[600-developer-tools-vscode-restclient-postman.md].
Related Concepts¶
- [[REST Client]]
- [[Postman]]
- HTTP Headers
- [[JSON Path]]
Sources¶
^[600-developer-tools-vscode-restclient-postman.md]