web stats
Base-64 Encode Entire TCP Message - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-09-2018, 01:32 PM
stuart stuart is offline
OBX.1 Kenobi
 
Join Date: Oct 2012
Posts: 28
stuart is on a distinguished road
Default Base-64 Encode Entire TCP Message

Hello,

I am using 3.6.1 and would like to encode an entire received TCP message and pass it to field in a JSON outgoing message. The message is HL7 with an encoded PDF in the OBX from another system. All examples I see here are file based solutions. Seems I should be able to have a simple JSON template set up, and drop the payload in a JSON field, encoded, and send out.

I think the key for me is this example:

msg = FileUtil.encode(payloadAll.toString().getBytes('UT F-8'));

... where i just drop this into my Message Builder step and replace "payloadAll" with my entire message ? Or is there something else needed ?

Thanks for any input !
Reply With Quote
  #2  
Old 10-09-2018, 09:34 PM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 822
siddharth is on a distinguished road
Default

FileUtil.encode is inbuilt, and does the same job. You could also use apache commons Base64() to encode your content. If you encode msg, you are encoding XML version of the message. You should encode connectorMessage.getRawData().

base64msg=FileUtil.encode(connectorMessage.getRawD ata().toString().getBytes('UT F-8'));

You mentioned encoded PDF in the message, so you are double encoding it. just FYI.
__________________
HL7v2.7 Certified Control Specialist!
Reply With Quote
  #3  
Old 10-10-2018, 06:15 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 476
agermano is on a distinguished road
Default

You can set your inbound type to Raw to avoid the extra overhead of converting the hl7 message to xml if you don't need to access any of the hl7 fields.

It won't change siddharth's solution, and yes, you can stick it directly in a message builder step to map to a JSON template field.
Reply With Quote
  #4  
Old 10-10-2018, 01:15 PM
stuart stuart is offline
OBX.1 Kenobi
 
Join Date: Oct 2012
Posts: 28
stuart is on a distinguished road
Default

Thank you siddharth !

I'm very close now! I think I'm missing a conversion somewhere. The response I'm getting back from the website is "Can not deserialize value of type byte[] from String". My code to encode is:
var base64msg = new String();
base64msg=FileUtil.encode(connectorMessage.getRawD ata().toString().getBytes('UTF-8'));

The encoded data has "\r\n", newline, throughout the message. That appears to be the problem because that doesn't happen when testing with Postman.

Thanks for further suggestions!
Reply With Quote
  #5  
Old 10-11-2018, 06:39 AM
stuart stuart is offline
OBX.1 Kenobi
 
Join Date: Oct 2012
Posts: 28
stuart is on a distinguished road
Default

This is my first time doing encoding in Mirth and it appears the newline characters are used to break the message into "bite-size" pieces for transmitting ?
Reply With Quote
  #6  
Old 10-11-2018, 06:56 AM
stuart stuart is offline
OBX.1 Kenobi
 
Join Date: Oct 2012
Posts: 28
stuart is on a distinguished road
Default

... also, when I decode the message that I sent, I see these characters throughout the message. "" . When i remove them, the message matches up to a NotePadd++ encoding, which passes the error checking.

Thanks for any direction here...
Reply With Quote
  #7  
Old 10-12-2018, 08:12 AM
stuart stuart is offline
OBX.1 Kenobi
 
Join Date: Oct 2012
Posts: 28
stuart is on a distinguished road
Default

Thank you siddharth and agermano !

Being my first time using encoding in Mirth, I see that the data gets chunked automatically. The system that I send to does not understand chunking. Did a String.replace() on '\r\n' and all is well.

Thanks !
Reply With Quote
  #8  
Old 10-16-2018, 05:06 AM
jonathanlent jonathanlent is offline
OBX.1 Kenobi
 
Join Date: Aug 2014
Location: Denton, TX
Posts: 37
jonathanlent is on a distinguished road
Default

There is an identity attachment processor that is designed to take the entire message and store it as a base64-encoded attachment. That would do the heavy lifting for you and then you could just use a JSON outbound template and the message attachment utilities to add the attachment to the outbound message. There are a couple different ways to achieve it but a transform like the following on the destination should work:

Code:
MyJSON.oayload = getAttachments(false).get(0).getContenetString();
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 01:22 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Mirth Corporation