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){
  console.log(err); 
    console.log(data); 
    console.log(res);
});

// stops the script from failing if nothing has loaded
$browser.get('https://your-url');

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.

2 Likes

@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: