0

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

2

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>
| improve this answer | |
  • Thank you. I do understand that the resulting object is from the catch. What I do not understand is why HTTP throws the error. There is no syntax error. You can use the code on nay cloudpage as it is to replicate this. – shd.lux Apr 28 at 4:18
  • @shd.lux edited my answer with more relevant info based on your comment – Gortonington Apr 28 at 13:30
  • thank you for the alternative solution which works great. Your answer confirms my finding but this means the documentation is wrong, does it not? "The function returns a JSON object containing a status value and the HTTP response" It does not mention anything of throwing an error except for a 200 response. – shd.lux Apr 29 at 14:28
  • 1
    @shd.lux I wouldn't necessarily say 'wrong' just more vague and misleading. I am hoping to work with a group of people to help further clarify SSJS documentation as an outside reference. But that is still quite a while away before it comes to be. – Gortonington Apr 29 at 15:14
  • that would be great (count me in if you need help). I assume many of the MVPs are involved hence early access on new releases will make this feasible. – shd.lux Apr 30 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.