HTTP sender in Javascript

This blog post is about developing HTTP sender in Mirth, without having to send your data via destination.

Suppose, consider a situation when we need to loop through the incoming message and we have to send/post the message multiple time. Mirth do not have the capability to store the loop content variable and pass that to the destination end. So, if you want to loop through the incoming message and send them to HTTP sender, we need to write the Javascript code in the source transformer.

Imagine, that “output” is the variable that contains the looped contents of information in HL7v2 format, now use the below code to invoke HTTTP sender via javascript.

do {
try {
var data = output;
//Destination URL
destURL = ‘http://localhost:8088/’;
//URL
var url = new java.net.URL(destURL);
var conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-length”, data.length());
conn.setRequestProperty(“Content-type”, “text/plain”);
var outStream = conn.getOutputStream();
var outWriter = new java.io.OutputStreamWriter(outStream);
outWriter.write(data);
outWriter.close();
// Get response Code (200, 500 etc.)
var respCode = conn.getResponseCode();
// get response body
//getResponse();
var response = new XML($(‘RESPONSE’)); // XML response message
var status = response[‘Status’].toString();

if (respCode != 200) {
// Write error to error folder
var stringData = response.toString() + ‘\n’;
FileUtil.write(‘C:/Outbox/Errors/’ + $(‘originalFilename’) + ‘.ERROR_RESPONSE’, false, stringData);
// Return Error to Mirth to move the file to the error folder
return ERROR;
}
errorCond = ‘false’;
break;
}
catch(err) {
retry++;
if(retry > 10) {
channelMap.put(‘RESPONSE’, err);
responseMap.put(‘WEBSVC’, ResponseFactory.getErrorResponse(err))
throw(err);
// Can return ERROR, QUEUED, SENT
// This re-queues the message on a fatal error. I’m doing this since any fatal message may be
// caused by HTTPS connect errors etc. The message will be re-queued
return QUEUED; // Re-queue the message
}
java.lang.Thread.sleep(6000); // 6 seconds * 10

errorCond = ‘true’;
}
}
while (errorCond == ‘true’);

Note : This code should be present inside the for loop or the while loop, so that the specific HL7v2 message will be sent/posted to the destination

 

 

Advertisements

6 thoughts on “HTTP sender in Javascript

Add yours

  1. How to add authentication to it?
    Have some trouble formating the basicAuth. Should be something like a base64 or so.
    conn.setRequestProperty(“Authorization”,basicAuth);

    Like

    1. Hi Matthy,

      It should be certainly possible to add the basic auth in this custom HTTP sender code. But I’m not sure if that can be added as you provided like conn.setRequestProperty();. I don’t have the working code in hand now.

      If you are looking for quick answer, use this link https://mirthconnect.slack.com/messages/C02SW0K4D/ this is online Mirth slack group. You can register free and ask your question there. People will be there to give you the answer immediately.

      Do post the reply once you have got the answer for this. I will also add the code for that in the same thread. In case I find to work on this.

      Thank you

      Like

  2. HI Guys,
    I have same code as above for http post request. Now i want to Mutual Transport Layer Security (mTLS) authentication for send http post request means want to send request with client certificate and password. i have client certificate, but i don’t know how to call post request in mirth for same. please help me

    Thanks,
    Yogesh

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: