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

Script.Util.HttpGet(1)

Properties

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

Ordinal Type Description
1 String Required. The URL to retrieve content from.

Methods

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

Method Description
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.

Property Type Description
continueOnError Boolean Specifies 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.
emptyContentHandling Number Indicates what to do if the GET request doesn’t return any content. Possible values: <ul><li>0 - Continue</li><li>1 - Stop the request</li><li>2 - Continue to the next subscriber. This option applies only to email sends.</li></ul>
retries Number The 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.

<script runat="server">
  /* Create an authentication string to pass as a request header */

  var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyFDTijiOiIxMjM0NTYexAmp1e";
  var auth = "Bearer " + token;

  try {
    /* Initialize the request handler */
    var request = new Script.Util.HttpGet("https://www.example.com/get");

    /* Set request headers */
    request.setHeader("Authorization", auth);
    request.setHeader("sample-header", "HeaderValue");

    /* Configure the request properties */
    request.retries = 2;
    request.continueOnError = true;
    request.emptyContentHandling = 1;

    /* Send the request */
    var response = request.send();

    /* Make sure the request resulting in a 200 OK status */
    if (response.statusCode == 200) {
      /* Format the content of the response body as a string, and then parse the
        JSON string. This step is necessary to output the content from the
        Script.Util.HTTPResponse object. */
      var responseStr = String(response.content);
      var responseJSON = Platform.Function.ParseJSON(responseStr);

      /* Output the response body */
      Platform.Response.Write(Platform.Function.Stringify(responseJSON));
    } else {
      Platform.Response.Write("Something went wrong");
    }
  } catch(e) {
    Platform.Response.Write(Platform.Function.Stringify(e));
  }
</script>

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

See Also