0

I'm using the following code in a Cloud Page to submit an HTTP Post request. I was able to get the Script.Util.HttpRequest() methods to work on their own. But now I want to only submit the request if the querystring contains a specific value. Every combination I've tried of running the script inside of an ampscript if statement results in a 500 error, and the debugging code isn't providing me with any information.

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

  %%[if RequestParameter('Q1')=="right" then]%%
<script runat="server">
  var req = new Script.Util.HttpRequest("{requestURL}");
  req.emptyContentHandling = 0;
  req.retries = 2;
  req.continueOnError = true;
  req.contentType = "application/x-www-form-urlencoded";
  req.method = "POST";
  req.postData = "{payload)";
var resp = req.send();

  Platform.Response.Write(resp.content);
  Platform.Response.Write(resp.headers["returnHeader"]);
</script>
%%[endif]%%
  
<script runat="server">
 }
 catch (err) {
  Variable.SetValue("@errorMessage", Stringify(err.message) + Stringify(err.description));
 }
</script> 
<script runat="client">
 console.log(`%%=v(@errorMessage)=%%`);
</script>
5
  • 2
    Why switch to AMPscript for the if statement? Commented Jun 25, 2020 at 20:36
  • 1
    Yes, as suggested by Gregory you can use Platform.Request.GetFormField('Q1'); Commented Jun 26, 2020 at 5:00
  • good input. is getFormField a recent addition? the article says lastUpdated Jun 16,2020. Commented Jun 26, 2020 at 6:44
  • I doubt console.log can be used in SSJS, and even if it was, you are using accents, not quotes, around your ampscript variable, which also can't be used like this in ssjs context. Commented Jun 26, 2020 at 7:07
  • getFormField has been around for quite a while - SSJS is just not really presented in a way to display its array of useful functions. And no you cannot use console.log in SSJS, but he opened a client-side block in his code there to display that. Commented Jun 26, 2020 at 12:36

2 Answers 2

1

I am not sure why you make a switch to AMPscript in the script. You can just run it all in SSJS:

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

  if (Request.GetFormField('Q1') == 'right') {

    var req = new Script.Util.HttpRequest("{requestURL}");
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = "application/x-www-form-urlencoded";
    req.method = "POST";
    req.postData = "{payload)";
    var resp = req.send();

    Write(resp.content);
    Write(resp.headers["returnHeader"]);
  }
  
 }
 catch (err) {
  Write('<script>console.log(' + Stringify(err.message) + Stringify(err.description)) + ')</script>');
 }
</script>

As you can see it is actually less code this way and should be much more performant. To handle your console.log part (since in a try/catch, the JS var would only be local and not global so could not be passed same way you passed AMPscript variable) I added the information into a Write() inside the catch block to output the client-side script to the page with the information you needed - which will then be performed and push that info to the console.

I removed runat="client" as I do not believe this is actual code and by default <script> is run at client-side, which is why for SSJS you need to specify runat=server.

0

Here is an error in syntax writing:

req.postData = "{payload)";

You have to write a form url encoded string, something like:

req.postData = "foo=bar&me=Daniel";

You must log in to answer this question.

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