Script.Util.HttpRequest
Full-featured HTTP request object supporting all methods, custom headers, timeouts, and full response inspection. The most powerful HTTP option in SSJS.
Script.Util.HttpRequest is the most flexible HTTP client available in SSJS. It supports all HTTP methods, custom headers, timeouts, and gives you full access to response status codes, headers, and body.
Script.Util.HttpRequest does not require Platform.Load. It is available in all SSJS contexts.
Syntax
var req = new Script.Util.HttpRequest(url);
req.method = "GET"; // HTTP method
req.contentType = "application/json"; // Content-Type for body
req.encoding = "UTF-8"; // Encoding (default UTF-8)
req.timeout = 30000; // Timeout in ms
req.setHeader(name, value); // Set a request header
req.postData = body; // Request body (POST/PUT/PATCH)
var resp = req.send();
Properties
| Property | Type | Default | Description |
|---|---|---|---|
method |
string | "GET" |
HTTP method: GET, POST, PUT, PATCH, DELETE |
contentType |
string | "" |
Content-Type header for body |
encoding |
string | "UTF-8" |
Character encoding |
timeout |
number | 30000 |
Timeout in milliseconds |
postData |
string | "" |
Request body (for POST/PUT/PATCH) |
Methods
| Method | Description |
|---|---|
req.setHeader(name, value) |
Add/set a request header |
req.send() |
Send the request; returns response object |
Response Object
The resp object returned by req.send() has:
| Property | Type | Description |
|---|---|---|
resp.statusCode |
number | HTTP status code |
resp.content |
CLR string | Response body (must use String() to convert) |
resp.headers |
object | Response headers |
resp.content is a CLR string, not a JavaScript string. Always wrap it with String(resp.content) before calling ParseJSON() or string methods.
Examples
GET request with auth
var token = Platform.Function.Lookup("Config", "accessToken", "key", "sfmcRest");
var req = new Script.Util.HttpRequest("https://mc.rest.example.com/v2/contacts");
req.method = "GET";
req.setHeader("Authorization", "Bearer " + token);
req.setHeader("Accept", "application/json");
try {
var resp = req.send();
if (resp.statusCode === 200) {
var data = Platform.Function.ParseJSON(String(resp.content));
Platform.Response.SetContentType("application/json");
Write(Stringify(data));
} else {
Platform.Response.SetResponseCode(resp.statusCode, "Upstream Error");
Write(Stringify({ error: resp.statusCode }));
}
} catch(e) {
Platform.Response.SetResponseCode(500, "Server Error");
Write(Stringify({ error: e.message }));
}
POST JSON body
var payload = Stringify({
DefinitionKey: "SomeJourneyKey",
ContactKey: subscriberKey,
EventDefinitionKey: "APIEvent-...",
Data: { FirstName: firstName, Plan: planType }
});
var req = new Script.Util.HttpRequest("https://mc.rest.example.com/interaction/v1/events");
req.method = "POST";
req.contentType = "application/json";
req.setHeader("Authorization", "Bearer " + token);
req.postData = payload;
var resp = req.send();
var result = Platform.Function.ParseJSON(String(resp.content));
PUT request (update)
var req = new Script.Util.HttpRequest("https://api.example.com/items/42");
req.method = "PUT";
req.contentType = "application/json";
req.setHeader("Authorization", "Bearer " + token);
req.postData = Stringify({ name: "Updated Name", active: true });
var resp = req.send();
DELETE request
var req = new Script.Util.HttpRequest("https://api.example.com/items/42");
req.method = "DELETE";
req.setHeader("Authorization", "Bearer " + token);
var resp = req.send();
With timeout
var req = new Script.Util.HttpRequest("https://slow.api.example.com/data");
req.method = "GET";
req.timeout = 10000; // 10 second timeout
req.setHeader("Authorization", "Bearer " + token);
var resp = req.send();
Complete REST API Helper Pattern
function callRestApi(method, url, token, body) {
var req = new Script.Util.HttpRequest(url);
req.method = method;
req.setHeader("Authorization", "Bearer " + token);
req.setHeader("Accept", "application/json");
if (body) {
req.contentType = "application/json";
req.postData = Stringify(body);
}
var resp = req.send();
var parsed = Platform.Function.ParseJSON(String(resp.content) + "");
return { status: resp.statusCode, data: parsed };
}
var result = callRestApi("GET", "https://api.example.com/v1/users", accessToken, null);
if (result.status === 200) {
Write("Users: " + result.data.count);
}