Make POST API calls
The API_POST extension sends a POST request to a REST API endpoint and returns the response body as a JSON string that can be stored in a Virtuoso variable.
This is useful when a journey needs to create a record, prepare backend test data, submit a payload to an API, or validate that the API response contains values created from the request body.
Parameters:
-
urlrequired, a string containing the REST API endpoint to call. Query parameters can be included directly in the URL when needed; -
bodyrequired, a JSON string containing the POST request payload. The extension parses this value withJSON.parse(body)before sending it; -
headersoptional, a JSON string containing request headers, such as authorization or content-type headers. Leave it blank when no custom headers are required.
Note: The body and headers inputs must be valid JSON strings when supplied. Invalid JSON, missing required values, or a response rejected by the API will fail the extension step through doneError.
How to apply this to your journey
Use the extension in a journey by calling API_POST with the URL and request body. Pass each value to the matching extension input using as url, as body, and optionally as headers.
Note: The extension returns JSON.stringify(data). Store the result in a variable and assert against the parsed response fields exposed by Virtuoso, such as $data.name.
API_POST("https://reqres.in/api/users", '{"name":"Virtuoso", "job":"Intelligent Quality Assistant"}') returning $data
assert $data.name equals "Virtuoso"API_POST("https://reqres.in/api/users", '{"name":"Virtuoso", "job":"Intelligent Quality Assistant"}', '{"Authorization":"Bearer example-token"}') returning $data
assert $data.job equals "Intelligent Quality Assistant"You can also pass variables into the extension when the endpoint, payload, or headers are created earlier in the journey.
API_POST($apiUrl, $requestBody) returning $data
assert $data.name equals "Virtuoso"store value "https://reqres.in/api/users" in $apiUrl
store value "Virtuoso" in $request.name
store value "Intelligent Quality Assistant" in $request.job
API_POST($apiUrl, $request) returning $data
assert $data.name equals "Virtuoso"Advanced API calls
Some APIs require custom request headers, such as authentication tokens, content type, tenant identifiers, or API-version headers. The API_POST extension supports this through the optional headers input. Pass headers as a valid JSON string or as a Virtuoso variable that resolves to a JSON object/string.
Content type example:
store value '{"content-type":"multipart/form-data"}' in $contentTypeHeader
API_POST("https://reqres.in/api/users", '{"name":"Virtuoso", "job":"Intelligent Quality Assistant"}', $contentTypeHeader) returning $data
assert $data.name equals "Virtuoso"Header authentication example, where the authentication token is already stored in $token:
store value ${'Bearer ' + $token} in $authenticationHeader.Authorization
API_POST("https://reqres.in/api/users", '{"name":"Virtuoso", "job":"Intelligent Quality Assistant"}', $authenticationHeader) returning $data
assert $data.name equals "Virtuoso"Example output:
{
"name": "Virtuoso",
"job": "Intelligent Quality Assistant",
"id": "123",
"createdAt": "2021-01-14T17:44:45.000Z"
}This extension requires the following resources:
The extension should be configured as:
- Run asynchronously: Yes
- Scope: Global
Limitation: This extension depends on the configured Axios CDN resource, valid JSON input, the target API, and browser-context network behavior. Because it uses axios.post, requests can be affected by CORS policy, authentication requirements, expired tokens, API validation rules, rate limits, network restrictions, proxy rules, blocked CDN access, content security policy, and API schema changes. The source parses body and headers with JSON.parse, so malformed JSON fails before the request is completed. The response is returned as a JSON string through done(JSON.stringify(data)); large or sensitive responses may be stored in journey variables and logs, so avoid returning secrets or unnecessary PII. Async extension scripts must complete within Virtuoso's documented 120-second maximum execution window, and the step fails if done() or doneError() is not reached. Cross-browser note: This extension does not inspect the page DOM, but it runs in the browser or device selected for the journey execution and uses Axios' browser request behavior. Validate it in each browser/device configuration used by your plans, especially where CORS handling, network access, CDN loading, corporate proxies, or mobile/remote-grid connectivity may differ.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 14/01/2021
Resources:
// Last updated: 14/01/2021, 17:44:45 UTC
// Resources:
// https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js
const makeRequest = async (url, body, headers = '') => {
try {
const { data } = await axios.post(
url,
JSON.parse(body),
headers ? {headers: JSON.parse(headers)} : {},
)
done(JSON.stringify(data))
} catch (e) {
doneError(e)
}
}
if (!url) {
throw new Error('URL parameter is missing')
}
if (!body) {
throw new Error('body parameter is missing')
}
makeRequest(url, body, headers)
Comments
0 comments
Please sign in to leave a comment.