Basic Scripts to Work with HTTP Request Properties in SSJS


I developed a script o make working with HTTP requests on CloudPages and in Code Resources easier.

<script runat="server">
Platform.Load("core", "1");

var requestData = {
        Method: Platform.Request.Method(),
        Browser: Platform.Request.Browser(),
        UserAgent: Platform.Request.UserAgent(),
        ClientIP: Platform.Request.ClientIP(),
        HasSSL: Platform.Request.HasSSL(),
        IsSSL: Platform.Request.IsSSL(),
        QueryString: Platform.Request.QueryString(),
        ReferrerURL: Platform.Request.ReferrerURL(),
        RequestURL: Platform.Request.RequestURL(),
        UserAgent: Platform.Request.UserAgent(),
        Authorization: Platform.Request.GetRequestHeader('Authorization'),
        PostData: Platform.Request.GetPostData('utf-8')
    }

Write(Stringify(requestData))
</script>

Of course feel free to edit and remove any properties as as necessary – you might not need properties in every script.

Example Response

This would be returned after hitting a code resource with this code in Postman:

{
    "Method": "PATCH",
    "Browser": {
        "Platform": "Unknown",
        "Browser": "Unknown",
        "Version": "0.0",
        "MajorVersion": 0,
        "MinorVersion": 0
    },
    "UserAgent": "PostmanRuntime/7.31.3",
    "ClientIP": "127.0.0.1",
    "HasSSL": true,
    "IsSSL": true,
    "QueryString": "",
    "ReferrerURL": null,
    "RequestURL": "https://subdomain.yourdomain.com/your-page",
    "Authorization": "Basic SGVsbG86U0ZTdGFja0V4Y2hhbmdl",
    "PostData": "{\r\n    \"hello\": \"world\"\r\n}"
}

HTTP Request Properties:

ItemDescription
Browser
Returns the following metadata regarding the browser from the Request object as a JSON object:
– Platform
– Browser
– Version
– Major Version Number
– Minor Version Number
Example: {“Platform”:”WinNT”,”Browser”:”Firefox”,”Version”:”9.0″,”MajorVersion”:”9″,”MinorVersion”:”.0″}
ClientIPReturns the IP address of the requesting client as a string value
HasSSLReturns a Boolean value indicating whether the current Request object could support SSL (HTTPS) usage, even if not currently using SSL
IsSSLReturns a Boolean value indicating whether the current Request object used an SSL (HTTPS) connection
MethodReturns the method associated with the Request object in a web context (such as GET or POST) in a string value
QueryStringReturns the full query string for the Request object as a string value
ReferrerURLReturns the URL of the referring web address in a web context in a string value
RequestURLReturns the full URL of the Request object as a string value

How to Parse Strings containing JSON Data Sent in a Request?

The script above can be used to get data sent to cloud pages or code resources. Just add this after the basic code script, but make sure you are retrieving the PostData property:

<script runat="server">
    Platform.Load("core", "1");

    var requestData = {
        PostData: Platform.Request.GetPostData('utf-8'),
        Method: Platform.Request.Method()
    }
    var requestPayload = Platform.Function.ParseJSON(requestData.PostData);
    
    Write(requestPayload.message);
</script>

Although the GetPostData function hast POST even in the name, it’s not the only HTTP request method supported by this function.

It works with any method including:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • OPTIONS
  • Custom methods that you define in tools like Postman – see this example with a made-up LEVELUP method:

🌱 Looks familiar?
This post is an expanded version of my answer to the “Retrieve Request Method from HTTP Request Header” question on the Salesforce Stack Exchange