web stats
Multipart form POST of base64 encoded PDF - SOLVED - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-09-2019, 08:13 AM
kirbykn2's Avatar
kirbykn2 kirbykn2 is offline
Mirth Guru
 
Join Date: Sep 2014
Location: Michigan
Posts: 607
kirbykn2 is on a distinguished road
Default Multipart form POST of base64 encoded PDF - SOLVED

I need to add text to the header, combine with a base64 encoded PDF and post to an HTTP Multipart form.

There are two transformer steps.

1. Creates the Boundary variable.
2. Does the rest of the work.

Code:
var hl7Content = connectorMessage.getRawData();

// assume 1 attachment per message

// Get the attachment and decode
var pdfContent = getAttachments(true).get(0).getContent();

var entity = org.apache.http.entity.mime.MultipartEntityBuilder
    .create()
    .setBoundary($('boundary'))
    //Content-Disposition: form-data; name="Case Record"; filename="18.pdf"
    .addBinaryBody($('documentType'), pdfContent, org.apache.http.entity.ContentType.create($('contentType')), $('filename'))
    .build();
    
var bos = new java.io.ByteArrayOutputStream();
entity.writeTo(bos);
msg = FileUtil.encode(bos.toByteArray());
On the destination, Multipart is set to NO, and the content encoding is binary.

Thanks again Agermano for helping with this one.
__________________
Best,

Kirby

Mirth Certified|Epic Bridges Certified|Cloverleaf Level 2 Certified

Appliance Version 3.11.4
Mirth Connect Version 3.8.0
Java Version 1.6.0_45-b06
Java (64 bit) Version 1.6.0_45-b06
Java 7 (64 bit) Version 1.7.0_151-b15
Java 8 (64 bit) Version 1.8.0_181-b13
PostgreSQL Version 9.6.8
Reply With Quote
  #2  
Old 01-07-2020, 12:50 PM
KChan049 KChan049 is offline
What's HL7?
 
Join Date: Jul 2017
Posts: 1
KChan049 is on a distinguished road
Default help

Would you be able to post your channel for this here?
I am having a similar issue and cant seem to get it to work.

Thank you!
Reply With Quote
  #3  
Old 01-25-2020, 06:28 AM
droptix droptix is offline
What's HL7?
 
Join Date: Jan 2020
Posts: 4
droptix is on a distinguished road
Question

It's also not working for me. I'm getting this error:
Code:
Can't find method org.apache.http.entity.mime.MultipartEntityBuilder.addBinaryBody(string,object,org.apache.http.entity.ContentType,string).
When I look at the docs, it should exist: https://hc.apache.org/httpcomponents...a.lang.String)
Code:
public MultipartEntityBuilder addBinaryBody(String name,
                                            byte[] b,
                                            org.apache.http.entity.ContentType contentType,
                                            String filename)
Also: what did you put into $('documentType'), $('contentType') and $('filename')?

I'm struggeling with this multipart topic now for more than 3 days and it seems that NONE of the examples and code snippets are working... but this seemed to be the cleanest solution for me.

My code is as follows:
Code:
logger.debug("destination msg: " + msg); // just to see that this is a base64 encoded string (coming from source connector which is a "File Reader")

var base64 = msg; // is a base64 encoded Java (not JavaScript!) string if source is "File Reader" and "File Type" is set to "Binary"

var boundary = '----------' + UUIDGenerator.getUUID();
$co('boundary', boundary); // make it available in destination as velocity template variable ${boundary} to be used in field "Content Type"

// http://www.mirthproject.org/community/forums/showthread.php?t=219080
// https://hc.apache.org/httpcomponents-client-4.5.x/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html#addBinaryBody(java.lang.String,%20java.io.File,%20org.apache.http.entity.ContentType,%20java.lang.String)

var name = 'file';
var contentType = org.apache.http.entity.ContentType.create('image/jpeg');
var fileName = 'IMG001.JPG';

var entity = org.apache.http.entity.mime.MultipartEntityBuilder.create()
	.setBoundary(boundary)
	//Content-Disposition: form-data; name="file"; filename="IMG001.JPG"
	//Content-Type: image/jpeg
	.addBinaryBody(name, base64, contentType, fileName)
	.build();
    
var bos = new java.io.ByteArrayOutputStream();
entity.writeTo(bos);
msg = FileUtil.encode(bos.toByteArray());
I'm using Mirth 3.5.1 (sorry for the old version, but I cannot update here so I have to use it)
Reply With Quote
  #4  
Old 01-25-2020, 06:52 AM
droptix droptix is offline
What's HL7?
 
Join Date: Jan 2020
Posts: 4
droptix is on a distinguished road
Smile

Sometimes it's strange... after many days (and nights) of research you decide to ask in the forums. And as soon as you did it, you get a new idea and finally found the answer on your own.

I want to share it with you, it's so easy (if you know how):

Destination Transformer:
Code:
var f = new java.io.File('C:\\Temp\\Images\\IMG001.JPG');

var boundary = '----------' + UUIDGenerator.getUUID();
$co('boundary', boundary); // make it available in destination as velocity template variable ${boundary} to be used in field "Content Type"

// http://www.mirthproject.org/community/forums/showthread.php?t=219080
// https://hc.apache.org/httpcomponents-client-4.5.x/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html#addBinaryBody(java.lang.String,%20java.io.File,%20org.apache.http.entity.ContentType,%20java.lang.String)

var name = 'file';
var contentType = org.apache.http.entity.ContentType.create('image/jpeg');
var fileName = 'IMG001.JPG';

// create multipart with the following data:
// Content-Disposition: form-data; name="file"; filename="IMG001.JPG"
// Content-Type: image/jpeg
var entity = org.apache.http.entity.mime.MultipartEntityBuilder.create()
	.setBoundary(boundary)
	.addBinaryBody(name, f, contentType, fileName)
	.build();
    
var bos = new java.io.ByteArrayOutputStream();
entity.writeTo(bos);
msg = FileUtil.encode(bos.toByteArray());
Destination settings:
  • Connector Type: HTTP Sender
  • Method: POST
  • Multipart: No (!!! this is very important !!!)
  • Response Content: Plain Body
  • Query Parameters: as required by your HTTP endpoint
  • Headers: as required by your HTTP endpoint
  • Content Type: multipart/form-data; boundary=${boundary}
  • Data Type: Binary
  • Content: ${message.encodedData}

.. to be optimized (dynamic image path and extracting the filename)

Working in Mirth 3.5.1
Reply With Quote
  #5  
Old 01-25-2020, 08:33 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,106
agermano is on a distinguished road
Default

I believe in your first example it should be
Code:
var base64 = FileUtil.decode(msg);
As you point out in your comment, msg is a base64 encoded string. You need to decode it to get a byte[] to pass to addBinaryBody.
Reply With Quote
  #6  
Old 02-05-2020, 02:45 AM
con con is offline
OBX.1 Kenobi
 
Join Date: Nov 2018
Location: Berlin
Posts: 42
con is on a distinguished road
Default

Stupid question, but is there any way to upload the Data/Bytearray outside of the Mirth by a file link, when the POST upload Method expect a multipart/form-data? I think about something like in curl with the option -F
Code:
curl -F ‘data=@path/to/local/file’ UPLOAD_ADDRESS
__________________
Work for a NPO health care provider in Berlin
  • Current Version of MC 3.4.1.770 with the onboard MySQL Database on a Windows Server 2012 R2

Last edited by con; 02-05-2020 at 02:55 AM.
Reply With Quote
  #7  
Old 02-05-2020, 09:06 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,106
agermano is on a distinguished road
Default

You can call curl from mirth, if that's what you mean?

Here's a code template to help with that https://github.com/nextgenhealthcare...time%20Command
Reply With Quote
  #8  
Old 02-10-2020, 07:38 AM
con con is offline
OBX.1 Kenobi
 
Join Date: Nov 2018
Location: Berlin
Posts: 42
con is on a distinguished road
Default

Ok, that might be an approach. I'll give it a try and test the responses / error handling with curl. Thanks for the hint
__________________
Work for a NPO health care provider in Berlin
  • Current Version of MC 3.4.1.770 with the onboard MySQL Database on a Windows Server 2012 R2
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 09:27 PM.


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