1

I would like to POST to an SFMC SSJS Landing page using AJAX.

Is there any way to access the POST JSON Request Payload using SSJS in the Landing page?

2 Answers 2

5

You of course want to get the raw data of the POST, and parse it using ParseJSON function. The example below is what you want to do.

<script runat="server" language="javascript">
Platform.Load("Core","1");
var jsonpost = Platform.Request.GetPostData()
var json = Platform.Function.ParseJSON(jsonpost);
var content = json.element.child;
Variable.SetValue("@profile",content);
</script>

%%=v(@profile)=%%

To access the body of a request, you use the getPostData method of the request object. This method takes as its parameter the number of characters of the body to return. If you specify 0, it returns the entire body. The return value is a string containing the requested characters. If there is no available data, the method returns the empty string. You can use this method to get all of the characters at once, or you can read chunks of data. Think of the body of the request as a stream of characters. As you read them, you can only go forward; you can’t read the same characters multiple times. To assign the entire request body to the postData variable, you can use the following statement:

postData = request.getPostData(0);

If you specify 0 as the parameter, the method gets the entire request. You can explicitly find out how many characters are in the information using the header’s content-length property, as follows:

length => parseInt(header["content-length"], 10);

To get the request body in smaller chunks, you can specify a different parameter. For example, the following code processes the request body in chunks of 20 characters:

var length = parseInt(header["content-length"], 10);
var i= 0;
while (i < length) {
postData = request.getPostData(20); 
// ...process postData... 
i = i +
20; }

Of course, this would be a sensible approach only if you knew that chunks consisting of 20 characters of information were meaningful in the request body.

2
  • 1
    Platform.Request.GetPostData() is exactly what I'm looking for, thanks Lukas! I couldn't (and still can't) find any documentation for this.
    – MIL
    Feb 22, 2018 at 10:49
  • 4
    The only place I have found it well documented is this PDF on SSJS, from Oracle: docs.oracle.com/cd/E19957-01/816-6411-10/816-6411-10.pdf See page 304 Feb 22, 2018 at 11:09
1

I was able to solve this by Base64 encoding a stringified JSON object client side, and appending the resulting string as a query parameter value. (Note we have to URL encode any '=' or the parameter will be ignored by SFMC SSJS)

<script>
    $.ajax({
        url: landpageURL + "?data=" + btoa(JSON.stringify(jsonObject)).replace('=', '%3d'),
        type: "POST",
        success: function (response) {
            // yay it worked
        }
    })
</script>

In SSJS we decode the string and parse the object;

<script runat="server">
    // https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/ssjs_syntaxGuide.htm
    Platform.Load("core", "1.1.1");

    var encodedString = Request.GetQueryStringParameter("data");
    var decodedJsonString = Platform.Function.Base64Decode(encodedString);
    var jsonObject = Platform.Function.ParseJSON(decodedJsonString);

    Write(Stringify(jsonObject))

</script>
3
  • btoa (Base64 encoding) is supported by all the major browsers; caniuse.com/#feat=atob-btoa
    – MIL
    Feb 22, 2018 at 9:03
  • Base64 encoding can result in 3 non web safe characters that need to be URL encoded; + = /
    – MIL
    Feb 22, 2018 at 9:04
  • Check up on the solution, where I used Platform.Request.GetPostData() shown above. I think it's more robust and requires less code. Feb 22, 2018 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.