Using the node-fetch Module

The node-fetch module is vitally useful in your transaction script for creating HTTP requests.

node-fetch is maintained and distributed by npm at https://www.npmjs.com/package/node-fetch. As such, you should always look there for authoritative information on usage and implementation.

This article describes some basic uses of node-fetch in ThousandEyes transaction scripts.

Importing node-fetch

To use the node-fetch module, make sure to first import it within your transaction script:

import fetch from 'node-fetch';

Making a Request

To make a request, use the fetch function:

await fetch('<url>');
<options>

The fetch function takes the following parameters:

  • url

    A string representing the URL that will receive the request.

  • options

    A JavaScript object that contains request parameters. For information on defaults, see the npm documentation.

    This parameter can be used to specify HTTP method, custom headers, timeout, and other settings.

Example POST Request

In the following example, you make a POST request and include a custom header:

await fetch('https://example.com',
{
method: 'POST',
headers: {'X-Custom-Header': 'Value-Here'}
}
)

Working with a Returned Promise

The fetch function returns a JavaScript promise.

To access the details of this response, use the await keyword before the fetch call, so that the promise can reach the fulfilled state:

let resp = await fetch('https://example.com');

The fulfilled promise is a Response instance, which has attributes and methods to read the response details:

console.log(resp.status); // HTTP Status Code
console.log(resp.headers.raw()); // HTTP Headers
console.log(await resp.text()); // HTTP Response Body

Example GET Request for a Promise

In the following example, notice that first the fetch function from the node-fetch module is imported. Then, when fetch is called, it is preceded by await so that the promise can reach the fulfilled state:

// fetch tx2.0
import fetch from 'node-fetch';
import assert from 'assert';
runScript();
async function runScript() {
let target = 'http://beta.the-acme-corporation.com';
let resp = await fetch(target, {method:'GET', headers: {'User-Agent': 'thousandeyes-transaction'}});
let resp_text = await resp.text()
assert(resp_text.includes('Welcome'), 'Did not find welcome text in response');
if(resp.status != 200){
throw Error('Non-200 response: ' + resp.status)
}
};

Advanced Examples

For additional examples, see the public repository of ThousandEyes transaction scripts.

Example: GET Request with Authentication

In the following example, notice that the script first defines requestOptions for basic authentication; then fetches the token from the response:

import {markers, credentials} from 'thousandeyes';
import fetch from 'node-fetch';
runScript();
async function runScript() {
const username = credentials.get('Peter Parker Username');
const token = credentials.get('Peter Parker Password');
var buffer = new Buffer(username+':'+token);
var apiToken = buffer.toString("base64");
var requestOptions = {
method: 'GET',
headers: {
'Authorization': 'Basic '+apiToken
}
}
markers.start('FetchTime');
const response = await fetch('https://api.thousandeyes.com/v6/agents.json?agentTypes=CLOUD', requestOptions);
const responseText = await response.text();
if (!response.ok) {
await console.log(responseText);
throw new Error('non-200 response');
}
markers.stop('FetchTime');
};

Example: Multiple GET Requests

In the following example, the first fetch request fetches a list of users; the second request fetches the activity log:

markers.start('Fetch User List');
let response = await fetch('https://api.thousandeyes.com/v6/users.json', requestBody);
let responseText = await response.json();
if (!response.ok) {
await console.log(responseText);
throw new Error('non-200 response while fetching User List');
}
markers.stop('Fetch User List');
markers.start('Fetch Activity Log');
response = await fetch('https://api.thousandeyes.com/v6/audit/user-events/search.json', requestBody);
responseText = await response.json();
if (!response.ok) {
await console.log(responseText);
throw new Error('non-200 response while fetching Activity Log');
}
markers.stop('Fetch Activity Log');

Example: Chained GET Requests with Authentication

In the following example, you find the list of agents assigned to a particular test; then extract the details for these agents:

let response = await fetch(`https://api.thousandeyes.com/v6/tests/${TEST_ID}.json`, requestBody);
let responseText = await response.json();
if (!response.ok) {
throw new Error('non-200 response while fetching Test details');
}
// Extract list of agents assigned to the given test
let test_agents = responseText['test'][0]['agents'];
let agent_details_promises = []
// Get details for individual Agents
for(var agent of test_agents){
markers.start('Agent Details ' + agent['agentId']);
agent_details_promises.push(fetch(`https://api.thousandeyes.com/v6/agents/${agent['agentId']}.json`, requestBody).then((value) => {
markers.stop('Agent Details ' + agent['agentId']);
return value;
}));
}
// Wait for all agent detail requests to complete
let agent_details = await Promise.all(agent_details_promises);
// Handle the response
for(var agent_detail_response of agent_details){
let agent_json = await agent_detail_response.json();
agent_json = agent_json['agents'][0]
}