Request Instance Methods
Reference for the instance methods and response object properties of Script.Util.HttpRequest and Script.Util.HttpGet — setHeader, clearHeaders, removeHeader, send, and the response object.
This page is a quick reference for the Script.Util.HttpRequest instance API: the methods you call on the request object before sending, and the properties available on the response object returned by send().
For full documentation including examples, see Script.Util.HttpRequest.
Request Object — Properties
Set these on the request object before calling send().
| Property | Type | Default | Description |
|---|---|---|---|
req.method |
string | "GET" |
HTTP method: GET, POST, PUT, PATCH, DELETE |
req.contentType |
string | "" |
Content-Type header value for the request body |
req.encoding |
string | "UTF-8" |
Character encoding |
req.timeout |
number | 30000 |
Request timeout in milliseconds |
req.postData |
string | "" |
Request body — used for POST, PUT, PATCH |
Request Object — Methods
| Method | Returns | Description |
|---|---|---|
<HttpRequestInstance>.clearHeaders() |
void | Remove all custom headers |
<HttpRequestInstance>.removeHeader(name) |
void | Remove a specific header by name |
<HttpRequestInstance>.send() |
HttpResponseInstance |
Send the request |
<HttpRequestInstance>.setHeader(name, value) |
void | Set a custom request header |
<HttpRequestInstance>.setHeader(name, value)
Adds or replaces a header on the outgoing request. Call it once per header.
var req = new Script.Util.HttpRequest("https://api.example.com/data");
req.method = "GET";
req.setHeader("Authorization", "Bearer " + token);
req.setHeader("Accept", "application/json");
req.setHeader("X-Custom-Header", "my-value");
var resp = req.send();
<HttpRequestInstance>.clearHeaders()
Removes all custom headers previously set on the request.
var req = new Script.Util.HttpRequest("https://api.example.com/data");
req.setHeader("Authorization", "Bearer " + token);
req.setHeader("Accept", "application/json");
req.clearHeaders(); // all custom headers removed
var resp = req.send();
<HttpRequestInstance>.removeHeader(name)
Removes a specific header from the request by name.
var req = new Script.Util.HttpRequest("https://api.example.com/data");
req.setHeader("Authorization", "Bearer " + token);
req.setHeader("X-Debug", "1");
req.removeHeader("X-Debug");
var resp = req.send();
<HttpRequestInstance>.send()
Sends the request and returns a response object. May throw on connection failure or timeout — wrap in try/catch for production code.
try {
var resp = req.send();
} catch (e) {
Write("Request failed: " + e.message);
}
Response Object — Properties
req.send() returns an object with the following properties:
| Property | Type | Description |
|---|---|---|
content |
CLR string | Response body (must use String() to convert) |
contentType |
string | The content type returned in the response |
encoding |
string | The encoding type returned in the response |
headers |
object | Response headers |
returnStatus |
number | A status value: 0 = OK, 1 = Empty URL, 2 = Call failed, 3 = Call succeeded with empty content |
statusCode |
number | HTTP status code |
resp.content is a CLR string object, not a native JavaScript string. Always convert it with String(resp.content) before passing to ParseJSON() or any string method.
Reading the response body
var resp = req.send();
var rawBody = String(resp.content); // Convert CLR string
var data = Platform.Function.ParseJSON(rawBody); // Then parse JSON
Checking the status code
var resp = req.send();
if (resp.statusCode === 200) {
var data = Platform.Function.ParseJSON(String(resp.content));
} else if (resp.statusCode === 404) {
Write("Not found.");
} else {
Write("Error: " + resp.statusCode);
}
Reading a response header
var resp = req.send();
var contentType = resp.headers["Content-Type"];
Full Example
var req = new Script.Util.HttpRequest("https://api.example.com/v1/data");
req.method = "POST";
req.contentType = "application/json";
req.timeout = 15000;
req.setHeader("Authorization", "Bearer " + accessToken);
req.setHeader("Accept", "application/json");
req.postData = Stringify({ key: "value" });
try {
var resp = req.send();
var body = Platform.Function.ParseJSON(String(resp.content));
if (resp.statusCode === 200 || resp.statusCode === 201) {
Write("Success: " + body.id);
} else {
Write("Unexpected status: " + resp.statusCode);
}
} catch (e) {
Write("Request error: " + e.message);
}