Script.Util.HttpGet()

Retrieves information from a URL by issuing an HTTP GET request. Unlike the Platform and Core GET functions, this request handler caches content for use in mail sends, which can improve the performance of email sends.

When you use the handler’s send() method, it returns a Script.Util.HttpResponse object that contains the response data.

This request handler only works with HTTP on port 80 and HTTPS on port 443.

Syntax 

1Script.Util.HttpGet(1)

Properties 

The Script.Util.HttpGet request handler has the properties listed in this table.

OrdinalTypeDescription
1StringRequired. The URL to retrieve content from.

Methods 

The Script.Util.HttpGet request handler has the methods listed in this table.

MethodDescription
clearHeaders()Removes all custom headers set for the request.
removeHeader(String)Indicates a specific header to remove from the request.
send()Sends the request and returns a response data object. The send() method times out after 30 seconds.
setHeader(String, String)Sets the headers to include in the GET request. If you use this property to specify a header, content caching is disabled.

Additional Properties 

The Script.Util.HttpGet request handler has the configuration properties listed in this table.

PropertyTypeDescription
continueOnErrorBooleanSpecifies how to handle errors. If the value is true, the request continues after receiving an error. If the value is false, the request produces an exception. The default value is false.
emptyContentHandlingNumberIndicates what to do if the GET request doesn’t return any content. Possible values:
  • 0 - Continue
  • 1 - Stop the request
  • 2 - Continue to the next subscriber. This option applies only to email sends.
retriesNumberThe number of times to retry the GET request if a failure occurs. After the final retry, the request produces an exception. The default value is 1.

Example 

To use this request handler, initialize it and provide the destination URL. This code example also shows how to specify some request headers and configure the parameters of the request.

1<script runat="server">
2  /* Create an authentication string to pass as a request header */
3
4  var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyFDTijiOiIxMjM0NTYexAmp1e";
5  var auth = "Bearer " + token;
6
7  try {
8    /* Initialize the request handler */
9    var request = new Script.Util.HttpGet("https://www.example.com/get");
10
11    /* Set request headers */
12    request.setHeader("Authorization", auth);
13    request.setHeader("sample-header", "HeaderValue");
14
15    /* Configure the request properties */
16    request.retries = 2;
17    request.continueOnError = true;
18    request.emptyContentHandling = 1;
19
20    /* Send the request */
21    var response = request.send();
22
23    /* Make sure the request resulting in a 200 OK status */
24    if (response.statusCode == 200) {
25      /* Format the content of the response body as a string, and then parse the
26        JSON string. This step is necessary to output the content from the
27        Script.Util.HTTPResponse object. */
28      var responseStr = String(response.content);
29      var responseJSON = Platform.Function.ParseJSON(responseStr);
30
31      /* Output the response body */
32      Platform.Response.Write(Platform.Function.Stringify(responseJSON));
33    } else {
34      Platform.Response.Write("Something went wrong");
35    }
36  } catch(e) {
37    Platform.Response.Write(Platform.Function.Stringify(e));
38  }
39</script>

The example outputs the full body of the response received from the server.

See Also