0

I am using the following code to make an API request to a third party system:

var req = new Script.Util.HttpRequest(URL_ENDPOINT);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json";
req.setHeader('grant_type', 'Basic ' + ENCODED_PASS);
req.method = "POST";
req.postData = Stringify(payload);

var resp = req.send();
var res = results.content;
Write(res + "<br>");
var json = Platform.Function.ParseJSON(String(res));

try {
    if (json.status == 200) {
        Write("SUCCESS!<br>");
    } else {
        Write("Errors found<br>");
        Write("Error code: " + json.status + "<br>");

    }

} catch (e) {
    Write("fatal error");
}

When running this, I get a fatal error with the following response from my Write(res) line:

    { "results": [ { "errors": [ { "message": "Key (phone)=(41792232493) already exists." } ], "status": 409, "index": 0 }, { "errors": [ { "message": "Key (phone)=(41792232492) already exists." } ], "status": 409, "index": 2 } ] }
fatal error
5
  • I could be reading it wrong, but this looks like the SSJS API call went through fine, the third party endpoint is the one throwing back the error - so debugging of the issue would need to be done there. Commented Mar 10, 2020 at 19:03
  • Hey sorry should of been more clear, I’m not expecting a 290 response. When I refer to json.status the exception is fired instead of going into my != 200 condition Commented Mar 10, 2020 at 19:06
  • 1
    It appears that status is not available as an overall return as you have it with json.status. status is instead on a per object basis inside the results array on each individual object. So would have to be returned via json.results[0].status to get status on first call and so on. Perhaps this is what is causing the error? Commented Mar 10, 2020 at 19:53
  • I believe null value in comparison can cause typeerrors which may be why the try is failing - stackoverflow.com/questions/23424673/… Commented Mar 10, 2020 at 20:00
  • 1
    You're right about the status, this is actually within the list of errors. I've wrongly assumed this is the StatusCode returned from the HTTP Response, using results.responseCode I was able to get the return I needed. Could you turn you're second comment in to the answer please. Thanks Commented Mar 10, 2020 at 22:24

1 Answer 1

0

It appears that status is not available as an overall return as you have it with json.status. status is instead on a per object basis inside the results array on each individual object.

So would have to be returned via json.results[0].status to get status on first call and so on. Perhaps this is what is causing the error?

You must log in to answer this question.

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