Assign variables
Capture values from responses, headers or request bodies into Postman collection variables to chain requests in Portman.
assignVariables sets Postman collection variables from a response body, response header, request body or a fixed value after a request runs. Combined with overwrites, it chains requests together, for example reusing the ID of a created record in the get, update and delete requests.
Example: create, then reuse the ID
{ "version": 1.0, "assignVariables": [ { "openApiOperation": "POST::/crm/leads", "collectionVariables": [ { "responseBodyProp": "data.id", "name": "leadsAdd.id" } ] } ], "overwrites": [ { "openApiOperation": "GET::/crm/leads/{id}", "overwriteRequestPathVariables": [ { "key": "id", "value": "{{leadsAdd.id}}", "overwrite": true } ] }, { "openApiOperation": "DELETE::/crm/leads/{id}", "overwriteRequestPathVariables": [ { "key": "id", "value": "{{leadsAdd.id}}", "overwrite": true } ] } ]}Make sure the create request runs before the requests that use the variable. You can control the request order with orderOfOperations in globals.
Options
| Property | Description |
|---|---|
openApiOperationId | OpenAPI operationId for which the variables are set. |
openApiOperationIds | Array of operationIds. |
openApiOperation | Method and path, supports wildcards. |
excludeForOperations | Optional. Operations to skip. |
collectionVariables | Array of variables to set. |
Each item in collectionVariables uses one source and an optional name:
| Property | Description |
|---|---|
responseBodyProp | Take the value from this response body property. Use . to store the whole body. |
responseHeaderProp | Take the value from this response header. |
requestBodyProp | Take the value from this request body property. |
value | A fixed value. Supports template expressions. |
name | Optional. The Postman variable name. Default: <operationId>.<varProp>. Supports template expressions. |
"assignVariables": [ { "openApiOperation": "GET::/crm/leads/{id}", "collectionVariables": [ { "responseBodyProp": "data.company_name" }, { "responseBodyProp": ".", "name": "leadResponse" }, { "responseHeaderProp": "Operation-Location" } ] }, { "openApiOperationId": "leadsAdd", "collectionVariables": [ { "requestBodyProp": "company_name", "name": "leadsAdd.company_name" } ] }]Template expressions
Variable names and overwrite values can use template expressions, so one rule can cover many operations. Using the operation GET /crm/leads with operationId leadsAll and tags Leads, CRM:
| Expression | Result |
|---|---|
<operationId> | leadsAll |
<method> | GET |
<path> | /crm/leads |
<pathRef> | GET::/crm/leads |
<opsRef> | The operationId, falling back to the pathRef when there is none. |
<tag> | First tag, Leads |
<tag1>, <tag2>, <tagn> | The nth tag. |
<pathPart1>, <pathPart2>, <pathPartn> | The nth path segment: crm, leads. |
Generic chaining for every resource, in YAML:
globals: variableCasing: snakeCaseassignVariables: - openApiOperation: POST::* collectionVariables: - responseBodyProp: data.id name: "<tag>Id"overwrites: - openApiOperation: "*::/crm/*/{id}" excludeForOperations: - POST::* overwriteRequestPathVariables: - key: id value: "{{<tag>Id}}" overwrite: trueAfter POST /crm/leads, the ID is stored as leads_id (the <tag>Id expression, converted by variableCasing), and every request on /crm/leads/{id} uses it. The same happens for contacts with contacts_id.
Logging
Assigned variable names are logged in the Portman and Postman console. Turn Portman's logging off with --logAssignVariables false.
In variations
assignVariables can also be defined inside a variation, without a target.