Performing HTTP requests in a Scripted Browser

You can extend the functionality of your Synthetics scripts with the help of Node.js modules

For example, you can perform a POST request to your endpoints with the urllib module as follows;

// Add in the library for performing POST requests
var urllib = require('urllib');

urllib.request('https://your-url', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  content: JSON.stringify({
    example: 'data'
}, function(err, data, res){

// stops the script from failing if nothing has loaded

As the Scripted Browser requires a page to be loaded, the last line will open the URL provided before finishing.
You can expand on this by using the assert module to validate your results.


@KarlC - I also included that little $browser.get workaround for my API Synthetic Maintenance Workaround script :wink:

BTW, would you really be using a Scripted Browser test or a Scripted API test for API calls? :innocent:

I think it is nice you can perform HTTP requests in a Scripted Browser test. Most of the time you’d probably want to use Scripted API but I was recently looking into it and because you as you can extend with Node, you could import the request library into a scripted browser if you wanted to.

My scenario, I want to write a test for one or more product pages, the key thing however is not hard coding a URL for the browser test, because there’s no guarantee this URL is going to exist forever and could therefore cause a “failure” which in fact it due to the URL being static, as products may come and go. So first, I want to make an API request to retrieve a valid and active product page URL from an API response that absolutely will return a 100% valid URL currently and then pass that to $browser.get to begin the test and perform the interaction elements.

For extra testing purposes, that API could be designed to randomise a series of URLs to regularly test random URLs that follow the same testing process for more visibility, then use console.log to log the parameters used in the test, in the event it does return a failure and then it will highlight what specific product page failed.