web stats
multipart POST with HTTP Sender - Page 2 - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #11  
Old 02-15-2017, 03:38 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 832
siddharth is on a distinguished road
Default

That is a channel exported to an xml file. You need to import it in your mirth box to know what nick is talking about.
Reply With Quote
  #12  
Old 02-15-2017, 06:04 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,097
narupley is on a distinguished road
Default

The XML file I attached was a channel group for version 3.4.2. Since you didn't specify I'm assumed you're using the latest version.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #13  
Old 03-15-2017, 01:53 PM
jnankin jnankin is offline
Mirth Newb
 
Join Date: Jan 2017
Posts: 6
jnankin is on a distinguished road
Default

I have downloaded your XML file and imported the Channel Group to take a look. correct me if I'm wrong, but it looks like this will only allow me to POST a file and nothing else. Furthermore, I cannot name the part of the POST that contains the file.

I need to add multiple parts to my POST (various text fields and a file) and the part that contains the file needs to be named a specific name.

Is that not possible with the latest version of Mirth?

To be specific, my POST (as mentioned earlier) needs to be formatted similar to the following:

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="inflation.txt"
Content-Type: text/plain

hi
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="to"

+18662495773
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="api_key"

REDACTED
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="api_secret"

REDACTED
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Reply With Quote
  #14  
Old 10-27-2017, 09:55 AM
maglf maglf is offline
What's HL7?
 
Join Date: Oct 2017
Posts: 1
maglf is on a distinguished road
Default

I appear to be battling this same topic. I am trying to post a small XML body + encoded PDF to an external API through Mirth. I am using Mirth Connect 3.4.2.

When I use a tool like Postman, I can make the post successfully, and the body looks like this:

Code:
POST /post/to/endpoint HTTP/1.1
Host: somecompany.com
Authorization: Basic UkVHYz5ING0uV2N0VXRqNDpDbVV7WDN6SmJmQDdCWVJr
Cache-Control: no-cache
Postman-Token: d5e11790-f11d-96f4-f2fe-870b4a3572ea
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data_message"

<result><code>ABC123</code></result>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="report_pdf"; filename="2FD15_ABC123.pdf"
Content-Type: application/pdf


------WebKitFormBoundary7MA4YWxkTrZu0gW--
This works perfectly. I presume that Postman is doing some magic behind the scenes to actually send off the binary file in that second segment, because I don't see the file contents in there.

I set up a sample endpoint in Python to receive this request, and I do see all of the data (I've left a bunch of fields out for security reasons):
Code:
{'_parsed_content_type': ('multipart/form-data',
                          {'boundary': '----WebKitFormBoundary7MA4YWxkTrZu0gW'}),
 'cookies': {'session': '0f65374f-a6c2-4ed3-acf5-6fc774d13a77'},
 'environ': {'CONTENT_LENGTH': '205609',
             'CONTENT_TYPE': 'multipart/form-data; '
                             'boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
             'HTTP_ACCEPT': '*/*',
             'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
             'HTTP_CACHE_CONTROL': 'no-cache',
             'HTTP_CONNECTION': 'keep-alive',
             'HTTP_HOST': 'somecompany.com',
             'HTTP_POSTMAN_TOKEN': '83a9edd1-c2fc-4b85-ad7a-f11dbcd87ff6',
             'HTTP_USER_AGENT': 'PostmanRuntime/6.4.1',
             'PATH_INFO': '/post/to/endpoint',
             'QUERY_STRING': '',
             'REQUEST_METHOD': 'POST',
             'SERVER_PROTOCOL': 'HTTP/1.1',
 'files': {'report_pdf': <FileStorage: '2FD15_ABC123.pdf' ('application/pdf')>},
 'form': {'data_message': '<result><code>ABC123</code></result>'},
 'shallow': False,
 'stream': <werkzeug.wsgi.LimitedStream object at 0x7f957bd5b2b0>,
 'url': 'http://somecompany.com/post/to/endpoint',
 'view_args': {}}
^^^ I can clearly see the files and form elements, which contains the data that I included in the post.

Now, in Mirth, I set up my channel per your example file that you posted.

I have:
  • URL: https://somecompany.com/post/to/endpoint
  • Use Proxy Server: No
  • Method: POST
  • Multipart: Yes
  • Response Content: Plain Body
  • Binary MIME Types: application/.*(?<!json|xml)$|image/.*|video/.*|audio/.*
  • Authentication: Yes
  • Authentication Type: Basic (and username/password set)
  • Query Parameters: (empty)
  • Headers: (empty)
  • Content Type: multipart/form-data; boundary=${boundary}
  • Data Type: Binary
  • Charset Encoding: Default (can't change it anyway)
  • Content: ${message.encodedData}

I have 2 transformers on this channel. The first one is for setting "boundary" and is an exact clone of what is in the example file.
The second has code that looks like this:

Code:
var xml_message = '<result><code>ABC123</code></result>;
var base64_pdf = msg['results']['report_pdf']

var payload1 = new java.lang.StringBuilder();
payload1.append('--').append($('boundary')).append('\r\n');
payload1.append('Content-Disposition: form-data; name="data_message"\r\n\r\n');
payload1.append(xml_message).append('\r\n');

payload1.append('--').append($('boundary')).append('\r\n');
payload1.append('Content-Disposition: form-data; name="report_pdf" filename="2FD15_ABC123.pdf"\r\n');
payload1.append('Content-Type: application/pdf\r\n\r\n');

var payload2 = new java.lang.StringBuilder();
payload2.append('\r\n');
payload2.append('--').append($('boundary')).append('--');

msg = FileUtil.encode(payload1.toString().getBytes('UTF-8'))
    + base64_pdf
    + FileUtil.encode(payload2.toString().getBytes('UTF-8'));
As you can see above, the message that's coming into this channel already has a base-64 encoded PDF under ['results']['report_pdf'] -- so no additional encoding is needed.

When I run my message through the channel, I see something like this in the output:

Code:
URL: https://somecompany.com/post/to/endpoint
METHOD: POST
USERNAME: myusername

[HEADERS]

[PARAMETERS]

[CONTENT]
LS0tLS0tLS0tLS0tNjM5NWQ2YjItM2U00110N2I0LTgxMjQtMjYyNjM0NmY5YmM0DQpDb250ZW50
LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImhsN21lc3NhZ2UiDQoNCjxobDdtZXNzYWdl
PjxkYXRhPjxyZXN1bHQ+asdfZGU+ODUyMTgzPC9jb2RlPjxvcmRlcl9jb2RlPjAxMjM8L29yZGVy
Mi0zZTdhLTQ3YjQtODEyNC0yNjI2MzQ2ZjliYzQNCkNvbnRlbnQtRGlzcG9zaXRpb246IGZvcm0t
ZGF0YTsgbmFtZT0iYW5hbHlzaXNfcGRmIiBmaWxlbmFtZT0icmlnaHRtZWQucGRmIg0KQ29udGVu
dC1UeXBlOiBhcHBsaWNhdGlvbi9wZGYNCg0K
JVBERi0xLjQKJeLjz9MKCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKL...
And if I inspect the request on the Python side, this is what I see:
Code:
{'cookies': {},
 'environ': {'CONTENT_LENGTH': '205936',
             'CONTENT_TYPE': 'multipart/form-data; '
                             'boundary=Sjj_p3_A_t4Qw5Dg_d2ZkyrQNPn98LQOvDJsXuvq',
             'HTTP_ACCEPT_ENCODING': 'gzip,deflate',
             'HTTP_CONNECTION': 'Keep-Alive',
             'HTTP_USER_AGENT': 'Apache-HttpClient/4.5.1 (Java/1.8.0_111)',
             'PATH_INFO': '/post/to/endpoint',
             'QUERY_STRING': '',
             'REQUEST_METHOD': 'POST',
             'SERVER_PROTOCOL': 'HTTP/1.1'
 'shallow': False,
 'url': 'http://somecompany.com/post/to/endpoint',
 'view_args': {}}
As you can see, this is quite a bit different than the one from Postman. I'm missing ALL of the form data and the file.

From what I understanding, setting the "Data Type"==Binary will assume that your message/content (stored in "msg") is a base-64 encoded string, and it will decode it BEFORE sending outbound.

However, how does this play out when I alreayd have a base-64 encoded PDF?

Is there something that I am missing in how I'm setting up my channel to handle multi-part POST requests? Has anyone else successfully implemented multi-part posts like this?

This seems like it should be much simpler than it is. Eek!

THANK YOU in advance for help that anyone can provide.
Reply With Quote
  #15  
Old 11-21-2017, 11:40 AM
jblaylock jblaylock is offline
What's HL7?
 
Join Date: Nov 2017
Posts: 1
jblaylock is on a distinguished road
Default

You can't concatenate base64 together like you are. See how your base64 has the random carriage return in there on the second to last line:
LS0tLS0tLS0tLS0tNjM5NWQ2YjItM2U00110N2I0LTgxMjQtMj YyNjM0NmY5YmM0DQpDb250ZW50
LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImhsN21lc3 NhZ2UiDQoNCjxobDdtZXNzYWdl
PjxkYXRhPjxyZXN1bHQ+asdfZGU+ODUyMTgzPC9jb2RlPjxvcm Rlcl9jb2RlPjAxMjM8L29yZGVy
Mi0zZTdhLTQ3YjQtODEyNC0yNjI2MzQ2ZjliYzQNCkNvbnRlbn QtRGlzcG9zaXRpb246IGZvcm0t
ZGF0YTsgbmFtZT0iYW5hbHlzaXNfcGRmIiBmaWxlbmFtZT0icm lnaHRtZWQucGRmIg0KQ29udGVu
dC1UeXBlOiBhcHBsaWNhdGlvbi9wZGYNCg0K
JVBERi0xLjQK
JeLjz9MKCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxvZwovUGFnZX MgMiAw

You need to combine the entire payload into one text blob and then base64 encode it.

Something like this:
var payloadAll = payload1.toString() + base64_pdf + payload2.toString()
msg = FileUtil.encode(payloadAll.toString().getBytes('UT F-8'));

If you include the base64 as text in the payload and then convert the whole payload to a byte array, when it gets decoded your encoded base64 will be present as text in the payload.
Reply With Quote
Reply

Tags
http, httpsender, multipart, post

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 12:04 AM.


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