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
status
is not available as an overall return as you have it withjson.status
. status is instead on a per object basis inside the results array on each individual object. So would have to be returned viajson.results[0].status
to get status on first call and so on. Perhaps this is what is causing the error?results.responseCode
I was able to get the return I needed. Could you turn you're second comment in to the answer please. Thanks