0

I am building an API call and I need to use POST. I decided to use 'Platform.Function.HTTPPost'. However, my cloudpage has 'internal server error' when I run the code.

var token = functionWhichRetrievesBearerToken() 
var journeyId = "X"
var journeyVersion = 1

var apiRoute = "RESTBaseURI/interaction/v1/interactions/stop/" + journeyId + "?versionNumber=" + journeyVersion

var contentType = "application/json";
var content = ""

var post = Platform.Function.HTTPPost(apiRoute, contentType, content, ["Authorization"], [token]);

Do you know what can cause the issue?

2 Answers 2

0

Update: like @Ghingis pointed out. The Platform.Function.HTTPPost function has 6 required parameters and you missed the last parameter.

The header value supposes to be Bearer YOUR_ACCESS_TOKEN as the Reference so you would want to modify your call like this:

var response
var token = functionWhichRetrievesBearerToken();
    token = 'Bearer ' + token;
var journeyId = "X";
var journeyVersion = 1; 

var apiRoute = "RESTBaseURI/interaction/v1/interactions/stop/" + journeyId + "?versionNumber=" + journeyVersion

var contentType = "application/json";
var content = ""

var post = Platform.Function.HTTPPost(apiRoute, contentType, content, ["Authorization"], [token], response);

if I were you, I would use Script.Util.HttpRequest function for the REST API call since it's more robust and flexible. So it would be something like this:

var token = functionWhichRetrievesBearerToken();
var auth = 'Bearer ' + token;
var journeyId = "X";
var journeyVersion = 1;
var url = 'https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/interaction/v1/interactions/stop/'+ journeyId + '?versionNumber=' + journeyVersion
var payload = ""

var req = new Script.Util.HttpRequest(url);
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = "application/json"
    req.setHeader("Authorization", auth);
    req.method = "POST"; /*** You can change the method here ***/
    req.postData = payload;

var resp = req.send();
2

First of all, I totally agree with Duc Le you should use Script.Util.HttpRequest instead of Platform.Function.HTTPPost.

But your actual issue is, that you missed the last parameter of HTTPPost

I recommend you wrap your code in a try-catch block during development / in cases you get a 500, it can save you some time and might be able to give you some feedback about your issue (not in this case though). But be careful, things can work wonky, like some function does not provide a response/work/work properly, if wrapped in a try-catch.

And let me loop back to first my statement why you should use Script.Util.HttpRequest instead, as you can see in Duc Le's example you can set a continueOnError property so it won't throw an exception if the request fails, so you have a way to handle that based on the response.

(You could also wrap the line of Platform.Function.HTTPPost in a try-catch block but that code will look atrocious.)

2
  • 1
    It seems I missed the last parameter in the function, thank you I will update like you pointed out in his first approach
    – Duc Le
    Mar 2, 2023 at 9:15
  • I just noticed you only declared the variable in your response, sadly that is not enough. It has to be an array and that array has to have at least one element. It can be an undefined or anything, you can even set the length of that array to 1 "manually", but otherwise, you won't get the response from your request. (The return value is just the status code of the response.)
    – Ghingis
    Mar 2, 2023 at 14:28

You must log in to answer this question.

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