Using the node-fetch module

The node-fetch module is vitally useful in your transaction script for creating HTTP requests. Transaction test scripts can use node-fetch to make requests against one or more HTTP API endpoints, and to chain data from one call to the next.

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.

If you need to run a test that solely consists of API calls, and don’t need to emulate a user journey in a browser, use the API Test Type.

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:

Arbitrary HTTP Request

You can use fetch() to make arbitrary HTTP requests in transaction scripts. Here is an example using fetch() to pull a webpage that requires Basic authentication (username and password are both "admin").

You can also pass different headers or use OAuth, although that is not shown in the example above.

fetch, net, and tls on Enterprise Agents

Cloud Agents have no known problems running the above script for arbitrary HTTP request, but if you run it on an Enterprise Agent, you might see that the transaction is timing out. In the event of a transaction timeout, you can find the following line in /var/log/te-sandboxd/te-sandboxd.log:

WARNING: IPv4 forwarding is disabled. Networking to destinations outside of the agent will not work.

Just because you see this message, doesn't mean you need to perform the following procedure. The following is intended to enable the use of node-fetch, net, and tls. If you are not using any of these, this warning may still appear, but it has no known impact.

In order to run this example script properly, ThousandEyes Enterprise Agents need to be configured to allow IPv4 forwarding. For TEVAs and Ubuntu packages, you'll need to edit /etc/sysctl.conf as follows:

This procedure involves system-level changes that require sudo.

sudo nano /etc/sysctl.conf

Uncomment the existing line, or add a line at the end that says:

net.ipv4.ip_forward=1

Save the changes and then tell sysctl to reload its settings:

sudo sysctl -p

That's it! Your tests should fetch() properly now.

If you follow Enterprise Agent Deployment Using Docker, you don’t need to explicitly enable IPv4 forwarding as described above, for Enterprise Agents running in Docker containers.

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:

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:

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:

Example: Multiple GET Requests

In the following example, the first fetch request fetches a list of users; the second request fetches the 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:

Proxy Support for API Monitoring

You can make API requests through a proxy by using fetch() and an HTTP proxy agent. This approach supports:

  • Configuring proxy settings for fetch() calls to auto-detect and apply transaction test settings

  • Trusting arbitrary certification chains

  • Tunneling connections through an arbitrary static proxy that might be using a self-signed certificate (e.g., for a decrypting TLS proxy)

  • Tunneling connections through a proxy determined by a PAC script

Supporting custom certificate chains is needed in instances where you need to run a fetch() command targeting a host for which a certificate was manually installed. For a custom certificate chain, copy the X509 of the certificate into your transaction script body.

The following examples are from the API transaction scripts folder of the ThousandEyes transaction scripting repository.

Example: Proxies with Custom Configuration

This ThousandEyes transaction script uses proxies with a custom configuration to call the endpoint with a self-signed certificate:

Example: Trusting Arbitrary Certificates

This ThousandEyes Transaction script calls the endpoint with a self signed certificate:

Example: Disable SSL Verification

This example uses a custom HTTPS agent with SSL verification disabled to call the endpoint with a self-signed certificate:

Example: Proxy With Custom SSL Certificate

This example uses a proxy and a custom SSL certificate.

Example: Proxy With SSL Disabled

This example uses a proxy with SSL disabled:

Example: Proxy With Test Configuration

This example uses a proxy from the ThousandEyes test configuration:

Example: Using Client Certificates

This example shows a transaction test script that uses client certificates:

Last updated