2

We are having an integration with an external REST API which we are invoking from a SSJS script hosted on a cloudpage. We have tried couple of ways to invoke the API Call - Script.Util.HttpRequest & Platform.Function.HTTPPost. In both cases when the external system returns a HTTP 200 code then the script is able to extract the JSON response data and process it. But when the external system returns any of these HTTP codes - 400, 403, 409, 410, then an exception gets raised and the control is transferred to catch section of the script. Inside the catch section we are unable to retrieve the response JSON data and the http code etc. All that we get is the SFMC internal exception that gets raised.

We were able to confirm this via invoking the same API via SOAP UI and here we are clearly able to see the different HTTP codes and the corresponding response JSON data.

As an example, below is the response received in SOAP UI for a particular data we send to the external system:

HTTP/1.1 409 Conflict Content-Type: application/json Content-Length: 73 Connection: keep-alive Date: Thu, 22 Aug 2019 06:00:05 GMT Cache-Control: no-store, no-cache X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=15768000 X-Download-Options: noopen X-XSS-Protection: 1; mode=block X-FRAME-OPTIONS: DENY X-Cache: Error from cloudfront Via: 1.1 2ca402c5412abcdc27e682e2b326bdbb.cloudfront.net (CloudFront) X-Amz-Cf-Pop: CDG3 X-Amz-Cf-Id: qB5AQNvHh7dqpe8HQbA6x0xc2kFOq5Tbj79IJvPOfFUEZQbpYM9hpg==

{"errorCode":"409.1","message":"You have already claimed this game code"}

Whereas the same data sent via the SSJS call returns this generic error message:

{"message":"An error occurred when attempting to evaluate a Util.HttpRequest call. See inner exception for details.\r\n URL: https://XXXXXXXXXXXXXXX/ext/v1/okta/prize/award\r\n Method: POST\r\n ClientID: 6235576\r\n JobID: 0\r\n ListID: 0\r\n BatchID: 0\r\n SubscriberID: 0\r\n","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a Util.HttpRequest call. See inner exception for details.\r\n URL: https://XXXXXXXXXXXXXXX/ext/v1/okta/prize/award\r\n Method: POST\r\n ClientID: 6235576\r\n JobID: 0\r\n ListID: 0\r\n BatchID: 0\r\n SubscriberID: 0\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: (409) Conflict. - from System\r\n\r\n\r\n\r\n"}

Can you please advise how we can retrieve the HTTP code and the JSON response data that is received, inside the SSJS script ?

Any inputs or suggestions would be highly appreciated.

TIA.

Keddy

1
  • I have experienced a similar scenario and couldn't resolve. I found it quite surprising that a generic exception is raised. Hoping someone can give an answer :) Commented Aug 28, 2019 at 5:04

1 Answer 1

2

You can get that, but you need to use ampscript and make your request with HTTPPost2.

Parameter 4 defines if it should fail on error. Set it to false. I'm not 100% sure what HTTP Status codes are counted as sucessful request, none the less if you make your request that way, you'll get status code returned as function output and request body will be in @output variable. You'll also need to transfer variables between ampscript and SSJS with these functions:

  1. Variable.GetValue()
  2. Variable.SetValue()
1
  • Thanks a ton Mateusz..your suggestion really solved my problem. Now the HTTP call does not cause an exception to be raised in the code. Instead I am able to properly process the HTTP return code and the response data and take appropriate actions accordingly.
    – Keddy
    Commented Aug 29, 2019 at 13:17

You must log in to answer this question.

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