OL Learn

POST http request from datamapper PostProcessor

Hi ,

I am running Connect 2020.2.1
I am trying to execute a simple HTTP POST request from the datamapper postprocessor:

var myjson = {“jobid”:“123”, “filedate”:“20210105”,“ids”:[,0,1,2,3],“keys”:[‘a’,‘b’,‘c’,‘d’],“products”:[]};
var request = createHTTPRequest();
var my_url = “http://localhost:9090/create_inventory?json=”+JSON.stringify(myjson);
request.setRequestHeader(‘Content-type’, ‘application/json; charset=UTF-8’);

But I get the below error in the datamapper on the request.send(); line

Error running postprocessors ( Wrapped java.lang.IllegalStateException: You cannot call getStatus() unless readyState == RECEIVING || LOADING

I have a NodeJS Server Input process which receives the request and Sends to Folder.
There is no trace of the request in Workflow.

Any ideas?

There are a number of issues here, both in your script and in the way Workflow handles these types of connections. The upcoming version of Connect will fix some of those issues. But in the meantime, here’s how you can make it work:

First of all, whenever you do a POST, you should send your payload in the body of the request, not as parameters in the URL. That is especially true if the data you are passing is a JSON string: a URL is limited in length, and the JSON may contain caracters that are invalid in a URL.

So instead, your code should look like this:

var myObject = {"jobid":"123", "filedate":"20210105","ids":[0,1,2,3],"keys":['a','b','c','d'],"products":[]};
var myJSON = JSON.stringify(myObject);

var request = createHTTPRequest();
var my_url = "http://localhost:9090/create_inventory";
request.setRequestHeader("Content-type", "application/json");

Notice how the URL now only contains the endpoint and the JSON objet is now passed as a parameter to the send() command. Note also that I removed the extra comma at the start of the ids[] array which was probably just a typo.

Now on the Workflow side, to receive this object, you’ll have to tick 2 options in your NodeJS Input task:

  • Loop through each attachment …
  • Discard XML envelope

That should get you going.

1 Like