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>