[SalesForce] SSJS HTTP.Post throws error instead of returning status code

I am banging my head against the HTTP.Post function.

The documentation says:

Performs an HTTP POST using the provided information against the passed URL. The function returns a JSON object containing a status value and the HTTP response.

This seems not to be the case…


Status code 200: all OK

var contentType = 'application/json',
    url = 'https://httpbin.org/status/200';

try{
    var req = HTTP.Post(url, contentType);
    Write(Stringify(req));
} catch(e){
    Write(Stringify(e));
}

Result:

{"StatusCode":200,"Response":[""]}


Status code 400: Throws error

var contentType = 'application/json',
    url = 'https://httpbin.org/status/400';

try{
    var req = HTTP.Post(url, contentType);
    Write(Stringify(req));
} catch(e){
    Write(Stringify(e));
}

Result:

{"message":"An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n – from Jint –> \r\n\r\n — inner exception 1—\r\n\r\nSystem.Net.WebException: The remote server returned an error: (400) Bad Request. – from System\r\n\r\n\r\n\r\n"}


I can fetch the error and handle it, but this is:

  • not what the documentation says
  • fetching the actual response will need regex and makes error handling harder

The same issue applies to Platform.Function.HTTPPost

Any ideas of why this is and if there is an alternative function which behaves differnetly?

Thank you

Best Answer

The resulting object you are getting there is from your Catch statement, not the HTTP.Post or HTTPPost error. This means that there is a syntax error that is causing the function to error, not an error on the server receiving the call.

What the two SSJS HTTP functions are referring to as an error is if the call returns an issue from the server (server says its incorrect customerkey or id or something). A syntax error will cause a '500' error on your page if you did not have your try/catch set up. What is returned there is your 'e' value grabbed in the catch.


EDIT: To answer your comment, any error response causes the function to error. Pretty much if you do not get a 200 response from the server, the POST function(s) will error and cause the page/script to fail.

You can get around this by using the Script.Util.HttpRequest function. This will allow you to return status and content regardless if there is an error code returned or not. Here is an article on my blog about using this function.

Sample:

<script runat=server>
Platform.Load("Core","1");
var contentType = 'application/json';
var url = 'https://httpbin.org/status/500';

var req = new Script.Util.HttpRequest(url);
  req.emptyContentHandling = 0;
  req.retries = 2;
  req.continueOnError = true;
  req.contentType = "application/json"
  req.method = "POST";

try {
    var resp = req.send();
    var resultStatusStr = String(resp.statusCode);
    var resultContentStr = String(resp.Content);
    var resultStatusJSON = Platform.Function.ParseJSON(resultStatusStr);
    var resultContentJSON = Platform.Function.ParseJSON(resultContentStr);
    Write('<br>resultStatusJSON: ' + Stringify(resultStatusJSON));
    Write('<br>resultContentJSON: ' + Stringify(resultContentJSON));
} catch(e) {
    Write('<br>Error: ' + Stringify(e));
}
</script>
Related Topic