0

Requirement -:
We need to Pass JSON Payload from one CloudPage to JSON Code resource via HTTPPOST AMPscript method and need to execute Final HTTPPOST2 function on JSON code resource to send that payload to an external API at backend to improve Performance of Thank you Page loading on web-form submission.

We are going with this approach as we need to call External API call using HTTPPOST2 ampscript function via JSON code resource to quick load of Thank you Page

Solution we are trying

So there will be 3 Components as below

  1. Web-form (main-cloudpage)
  2. Thank you Page (Landing-Page with HTTPPOST ampscript to post payload to Json code resource)
  3. JSON (Code resource to process final HTTPPOST2 call to external api-endpoint

Code on webform

HTML FORM with Post method we are posting FirstName , LastName and Email values entered in Web form

Code on thank you Page (Cloduapge)


  
%%[

set @FirstName = RequestParameter('FirstName')
set @LastName = RequestParameter('LastName')
set @email = RequestParameter('email')

set @authurl = "https://staging/SampleEndpoint/"
set @@payload = Concat('{"FirstName": "',@FirstName,'","LastName": "',@LastName,'","Email": "',@email,'"}')
set @key = "xxxxxxxxx-secret-key"

SET @httppost = HTTPPost(CloudPagesURL(1267),"application/json",@payload, @resp, 'Accept-Encoding', 'identity')


]%%

Code on JSON code resource


%%[ set @JSON = HttpGET("https://URL-of-thankyou-cloudPage/thank-you") ]%% 

<script runat="server">
    Platform.Load("Core", "1")
    // get JSON from ampscript
  var jsonObj = Variable.GetValue("@JSON");
  var obj = Platform.Function.ParseJSON(jsonObj);
  var Fname = obj.FirstName;
  var Lname= obj.LastName;
  var email= obj.Email;
  Platform.Variable.SetValue("@Fname",Fname);
  Platform.Variable.SetValue("@Lname",Lname);
  Platform.Variable.SetValue("@email",email);
</script>

%%[

set @FirstName = @firstnamee
set @LastName = @Lname
set @email = @email

set @authurl = "https://staging/SampleEndpoint/"
set @@payload = Concat('{"FirstName": "',@FirstName,'","LastName": "',@LastName,'","Email": "',@email,'"}')
set @key = "xxxxxxxxx-secret-key"

SET @httppost = HTTPPost2(@authurl,"application/json",@payload,false,@apistatusCode,@respheader,"Key",@key)


]%%


**Issue / challenges **

  • We are not able to Pass JSON payload from One Cloud page to JSON code resource
  • We are not able to Parse JSON on JSON code resource

Additional Query

  • Can we directly pass JSON PAyload from cloudpage to code resource and directly use that Payload in HTTPPOST2 AMPScript function ?

1 Answer 1

1

The major flaw I see is that instead of doing a GET on your Final page, you will want to use Platform.Request.GetPostData() from SSJS to retrieve the data posted. The HTTPGet will just get the final rendered page with no context (so basically the default HTML of your Thank You page).

By adding in the GetPostData() along with a Platform.Function.ParseJSON() you should then turn your passed JSON string into a functional JSON object that you can parse through.

There was some unnecessary actions so I made some changes. I personally would just utilize SSJS to make the API call instead of switching contexts as JSON is more natively supported there as well as it removes the performance degredation of switching Server Side languages, but I left it intact on your code below. So something like the below should work:


<script runat="server">
    Platform.Load("Core", "1")
    // get JSON from POST
  var postData = Platform.Request.GetPostData();
  Platform.Variable.SetValue("@payload",postData);
</script>

%%[

set @authurl = "https://staging/SampleEndpoint/"
set @key = "xxxxxxxxx-secret-key"

SET @httppost = HTTPPost2(@authurl,"application/json",@payload,false,@apistatusCode,@respheader,"Key",@key)


]%%

As the postData returned is a JSON string, there is no need to parse it and then rebuild it, so you can just directly pass it to AMPscript then use it in your function.

All in all, I am unsure if this will actually help your processing speed as I believe the POST to the code resource from the thank you page will need to wait for a return (which will not come as the load of that page will wait for the post on the code resource) essentially just adding an extra step to your process.

3
  • Thanks @Gortonington "Platform.Request.GetPostData()" worked and am able to use on code resource, Also as you mentioned it is taking time to load page - no improvement in Loading Thank you Page . I will check if there is any issue with API endpoint which we are calling from HTTPPOST2 function , Thanks
    – JNC
    Commented Aug 5, 2022 at 17:41
  • 1
    Instead of Platform.Function.GetPostData(); we need to use "Platform.Request.GetPostData()" - Must be typo in Answer :)
    – JNC
    Commented Aug 5, 2022 at 17:42
  • Apologies. WIll adjust! Commented Aug 5, 2022 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .