AMPscript
Platform.Function.HTTPGet()
Platform.Function.HTTPPost()
Script.Util.HttpGet
Script.Util.HttpResponse
Script.Util.HttpRequest
Validate Your Server-Side JavaScript using WSProxy
Performs an HTTP request against a specified URL. When you use this request handler’s send() method, it returns a Script.Util.HttpResponse object that contains the response data. Unlike the HTTP request functions in the SSJS Core and Platform libraries, you can use this handler to perform other types of HTTP requests, such as PUT, PATCH, or DELETE.
This request handler only works with HTTP on port 80 and HTTPS on port 443.
1Script.Util.HttpRequest(1)| Ordinal | Type | Description |
|---|---|---|
1 | String | Required. The URL to send the HTTP request to. |
The Script.Util.HttpRequest request handler has the methods listed in this table.
| Method | Description |
|---|---|
clearHeaders() | Removes all custom headers for the request. |
removeHeader(String) | Removes a specific custom header from the request. |
send() | Sends the request and returns a Script.Util.HttpRequest object. This method times out after 30 seconds. |
setHeader(String, String) | Adds a custom headers to the request. If you use this method to specify a header, content caching is disabled. |
The value of the host header is always set to the domain of the URL that the request was sent to, and the value of the content-length header is always set to the number of bytes of data in the request body. You can’t use the setHeader() method to modify the values of either of these headers.
Note
The Script.Util.HttpRequest request handler has the configuration properties listed in this table.
| Property | Type | Description |
|---|---|---|
contentType | String | The content type to use in requests, such as application/json or application/x-www-form-urlencoded. |
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. |
emptyContentHandling | Boolean | Indicates what to do if the request doesn’t return any content. If the value is false, the request throws an exception. If the value is true, the call continues without throwing an exception. |
encoding | String | The content encoding to use in requests, such as UTF-8. |
method | String | The HTTP verb to use in the request. Possible values:
|
postData | String | The request body. This property is required for POST requests. |
retries | Number | The number of times to retry the request. After the final retry, the request throws an exception. |
To use this request handler, initialize it and provide the destination URL. Next, specify the properties of the request, such as the HTTP method to use and the content of the request body. This example issues a PUT request to a destination server.
1<script runat="server">
2 /* Create an authentication string to pass as a request header */
3 var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyFDTijiOiIxMjM0NTYexAmp1e";
4 var auth = "Bearer " + token;
5
6 /* Specify the request body as a string */
7 var requestBody = '{"email": "ralph.vasquez@example.com","customerKey": "b714fdda-4a05-483c-972c-e04c9452b3aa"}';
8
9 try {
10 /* Initialize the request handler */
11 var request = new Script.Util.HttpRequest("https://www.example.com/put");
12
13 /* Set request headers */
14 request.setHeader("Authentication", auth);
15 request.setHeader("sample-header", "HeaderValue");
16
17 /* Configure the request properties */
18 request.method = "PUT";
19 request.encoding = "UTF-8";
20 request.postData = requestBody;
21 request.contentType = "application/json";
22
23 /* Send the request */
24 var response = request.send();
25
26 /* Format the response content as a string, and then parse the JSON in that
27 string. This step is necessary to output the content of the
28 Script.Util.HTTPResponse object. */
29 var responseStr = String(response.content);
30 var responseJSON = Platform.Function.ParseJSON(responseStr);
31
32 /* Output the response body */
33 Platform.Response.Write(Platform.Function.Stringify(responseJSON));
34 } catch(e) {
35 Platform.Response.Write(Platform.Function.Stringify(e));
36 }
37</script>The example outputs the full body of the response received from the server.