OL Learn

POST Command to API

Hi

I have a hybrid mail application which I want to submit files with a POST command and also issue a POST command to see how many items are queued up. I have very limited experience in calling API’s but someone has tested the API and said they can connect and issue the command:

Post command
{
“statuses”: [“DRAFT”, “HANDLED”, “ERROR”, “REJECTED”, “PENDING_VALIDATION”, “PENDING_COLLECTION”, “PENDING_DELIVERY”],
“username”: “admin”

}

Response:
{

"data": {
    "count": 10,
    "categories": {
        "DRAFT": 0,
        "HANDLED": 0,
        "ERROR": 3,
        "REJECTED": 0,
        "PENDING_VALIDATION": 0,
        "PENDING_COLLECTION": 0,
        "PENDING_DELIVERY": 7
    }
}

}

Where do I begin with doing this in workflow? I’m wanting to take the returned JSON and send a email out detailing how many items are PENDING_DELIVERY

Thanks

James

You’ll have to use a script for that. Here’s a basic version that issues a POST request and writes the response to the current job file (thereby replacing it) in Workflow:

// Initialize a few objects
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
var fso = new ActiveXObject("Scripting.FileSystemObject");

var payload = {
   "statuses": ["DRAFT", "HANDLED", "ERROR", "REJECTED", "PENDING_VALIDATION", "PENDING_COLLECTION", "PENDING_DELIVERY"],
   "username": "admin"
}
// specify the proper URL for the API call
xhr.open("POST", "http://someUrl.example.com/ApiEndpoint", false);
// send the payload object
xhr.send(JSON.stringify(payload));

// check if the call was successful
if(xhr.status==200) {
  res = xhr.responseText;
  // Overwrite Workflow's Job file with the response from the API call
  var myFile = fso.CreateTextFile(Watch.GetJobFileName());
  myFile.Write(res);
  myFile.Close();
}

You’ll probably have to tweak this a little, but it should get you going.

thanks Phil that’s perfect