Platform.Request provides methods to inspect every aspect of the incoming HTTP request in CloudPage, JSON Resource, and Triggered Send contexts.

Properties

Property Type Description
Platform.Request.Method string HTTP method: "GET" or "POST"

Methods

Method Returns Description
GetQueryStringParameter(name) string Read a URL query parameter
GetFormField(name) string Read a form field (POST or GET)
GetPostData([encoding]) string Read raw POST body (optional character encoding)
GetRequestHeader(name) string Read a request header
GetCookieValue(name) string Read a cookie value
GetUserLanguages() string Read the browser Accept-Language header value
GetResolvedURL() string Get the full resolved URL of the current page

Method: GetQueryStringParameter

Platform.Request.GetQueryStringParameter(parameterName)

Returns the value of a URL query string parameter. Returns "" if not present.

Parameters

Name Type Required Description
parameterName string Yes Query parameter name

Examples

// URL: /page?id=42&mode=preview
var id = Platform.Request.GetQueryStringParameter("id");     // "42"
var mode = Platform.Request.GetQueryStringParameter("mode"); // "preview"
var missing = Platform.Request.GetQueryStringParameter("foo"); // ""

Method: GetFormField

Platform.Request.GetFormField(fieldName)

Reads a field from either a GET query string or POST form body (application/x-www-form-urlencoded or multipart/form-data).

Some references use the name GetFormData for the same behavior; treat it as an alternate identifier for this request API.

Examples

var email = Platform.Request.GetFormField("email");
var firstName = Platform.Request.GetFormField("firstName");

Method: GetPostData

Platform.Request.GetPostData([encoding])

Returns the raw POST body as a string. Typically used for JSON or XML payloads sent with content-type application/json.

When encoding is omitted, the platform default applies (often a legacy Windows code page). Pass an encoding name such as "UTF-8" when the client sends UTF-8.

Examples

// CORRECT — read once, reuse the variable
var rawBody = Platform.Request.GetPostData();
var payload = Platform.Function.ParseJSON(rawBody + "");

// WRONG — second call returns ""
var a = Platform.Request.GetPostData();
var b = Platform.Request.GetPostData(); // b === ""

Checking Request Method First

if (Platform.Request.Method === "POST") {
    var rawBody = Platform.Request.GetPostData();
    if (!Platform.Function.Empty(rawBody)) {
        var data = Platform.Function.ParseJSON(rawBody + "");
        // process data
    }
}

Method: GetUserLanguages

Platform.Request.GetUserLanguages()

Returns the raw value of the HTTP Accept-Language header (for example a comma-separated list with quality values). Returns null when the header is absent.

Examples

var langs = Platform.Request.GetUserLanguages();
if (langs) {
    Write("<!-- Accept-Language: " + langs + " -->");
}

Method: GetRequestHeader

Platform.Request.GetRequestHeader(headerName)

Returns the value of an HTTP request header. Header names are case-insensitive.

Examples

var contentType = Platform.Request.GetRequestHeader("Content-Type");
var authHeader = Platform.Request.GetRequestHeader("Authorization");
var customToken = Platform.Request.GetRequestHeader("X-API-Token");

// Token authentication pattern
var token = Platform.Request.GetRequestHeader("X-Auth-Token");
var expectedToken = Platform.Function.Lookup("Config", "value", "key", "apiToken");
if (token !== expectedToken) {
    Platform.Response.SetResponseCode(401, "Unauthorized");
    Write(Stringify({ error: "Unauthorized" }));
    return;
}

Method: GetCookieValue

Platform.Request.GetCookieValue(cookieName)

Returns the value of a cookie sent with the request.

Examples

var sessionId = Platform.Request.GetCookieValue("sfmc_session");
if (Platform.Function.Empty(sessionId)) {
    // No session — redirect to login
    Platform.Response.Redirect("/login");
}

Method: GetResolvedURL

Platform.Request.GetResolvedURL()

Returns the full URL of the current CloudPage as it was resolved, including CloudPages URL encryption parameters.

Examples

var currentUrl = Platform.Request.GetResolvedURL();

Complete Request Handler Pattern

<script runat="server">
var method = Platform.Request.Method;

if (method === "GET") {
    var id = Platform.Request.GetQueryStringParameter("id");
    if (Platform.Function.Empty(id)) {
        Platform.Response.SetResponseCode(400, "Bad Request");
        Write(Stringify({ error: "id is required" }));
    } else {
        var record = Platform.Function.Lookup("Records", "data", "id", id);
        Platform.Response.SetContentType("application/json");
        Write(Stringify({ id: id, data: record }));
    }
} else if (method === "POST") {
    var rawBody = Platform.Request.GetPostData();
    try {
        var body = Platform.Function.ParseJSON(rawBody + "");
        // process body...
        Platform.Response.SetContentType("application/json");
        Write(Stringify({ status: "ok" }));
    } catch(e) {
        Platform.Response.SetResponseCode(400, "Bad Request");
        Write(Stringify({ error: "Invalid JSON" }));
    }
}
</script>

See Also