Platform.Response lets you control the HTTP response sent back to the browser. Useful for REST-style CloudPage APIs, redirects, and cookie management.

Methods

Method Returns Description
SetResponseCode(code [, description]) void Set HTTP status code
SetContentType(mimeType) void Set the Content-Type header
SetCookie(name, value [, expires [, path [, domain [, secure]]]]) void Set a response cookie
Redirect(url [, permanent]) void Redirect the browser

Method: SetResponseCode

Platform.Response.SetResponseCode(statusCode [, statusDescription])

Sets the HTTP response status code. Call this before writing any output.

Parameters

Name Type Required Description
statusCode number Yes HTTP status code (200, 400, 401, 403, 404, 500, etc.)
statusDescription string No Optional status text

Examples

Platform.Response.SetResponseCode(200, "OK");
Platform.Response.SetResponseCode(400, "Bad Request");
Platform.Response.SetResponseCode(401, "Unauthorized");
Platform.Response.SetResponseCode(404, "Not Found");
Platform.Response.SetResponseCode(500, "Internal Server Error");

API Response Pattern

Platform.Response.SetContentType("application/json");
if (!isAuthenticated) {
    Platform.Response.SetResponseCode(401, "Unauthorized");
    Write(Stringify({ error: "Authentication required" }));
} else {
    Platform.Response.SetResponseCode(200, "OK");
    Write(Stringify({ data: result }));
}

Method: SetContentType

Platform.Response.SetContentType(mimeType)

Sets the Content-Type header of the response. Default is text/html.

Common Content Types

Use Case MIME Type
JSON API response "application/json"
HTML page "text/html; charset=UTF-8"
Plain text "text/plain"
XML "application/xml"
JavaScript "application/javascript"

Examples

// JSON API
Platform.Response.SetContentType("application/json");
Write(Stringify({ status: "ok", count: rows.length }));

// Plain text
Platform.Response.SetContentType("text/plain");
Write("OK");

Method: SetCookie

Platform.Response.SetCookie(name, value [, expires [, path [, domain [, secure]]]])

Sets a cookie in the response.

Parameters

Name Type Required Description
name string Yes Cookie name
value string Yes Cookie value
expires string No Expiration datetime string
path string No Cookie path (default: "/")
domain string No Cookie domain
secure boolean No Send only over HTTPS

Examples

// Session cookie (expires when browser closes)
Platform.Response.SetCookie("sessionToken", token);

// Persistent cookie with expiry
var expiry = Platform.Function.FormatDate(
    Platform.Function.DateAdd(Platform.Function.Now(), 30, "D"),
    "ddd, DD MMM YYYY HH:mm:ss",
    "en-US"
) + " GMT";
Platform.Response.SetCookie("rememberMe", userId, expiry, "/", "", true);

// Clear a cookie (set expired date)
Platform.Response.SetCookie("sessionToken", "", "Thu, 01 Jan 1970 00:00:00 GMT");

Method: Redirect

Platform.Response.Redirect(url [, permanent])

Redirects the browser to the specified URL.

Parameters

Name Type Required Description
url string Yes Destination URL
permanent boolean No true for 301 redirect, false/omitted for 302

Examples

// Temporary redirect (302)
Platform.Response.Redirect("https://example.com/thank-you");

// Permanent redirect (301)
Platform.Response.Redirect("https://new-domain.com/page", true);

// Conditional redirect
var isLoggedIn = !Platform.Function.Empty(Platform.Request.GetCookieValue("session"));
if (!isLoggedIn) {
    Platform.Response.Redirect("/login?next=" +
        Platform.Function.URLEncode(Platform.Request.GetResolvedURL()));
}

See Also