web stats
TCP Listener to handle both GET verification without body and POST with JSON body - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2018, 02:31 AM
leon leon is offline
Mirth Newb
 
Join Date: Jul 2013
Location: South Africa
Posts: 7
leon is on a distinguished road
Default HTTP Listener to handle both GET verification without body and POST with JSON body

Hi

I need to write a channel that can handle a subscription confirmation similar to
https://www.w3.org/TR/websub/#verification-details.

So it needs to listen for HTTP GET with a single parameter, some headers and no body
e.g. http://127.0.0.1:8090/myendpoint/?challenge=a1b2c3
and return the challenge value back as the body

It should also be able to listen for HTTP POST with no parameters, some headers and a JSON body.

If I create a channel with a HTTP listener with inbound data type JSON and add any 1 step to the source transformer the POST works, but the GET fails because of the generated source transformer code with blank JSON message.

Using XML as inbound data type will not crash the the source transformer for a GET, but then the mapping for the JSON POST do not work properly.
HTTP listener with JSON inbound data type and no source transformer works for GET without a body.

Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransforme rException:
CHANNEL: TestListener
CONNECTOR: sourceConnector
SCRIPT SOURCE:
SOURCE CODE:
94: }
95: eval('importPackage(' + Packages.java.lang.Class.forName(className).getPac kage().getName() + ')');
96: }
97: }
98: function doScript() {
99: msg = JSON.parse(connectorMessage.getTransformedData());
100: tmp = JSON.parse(template);
101: function doFilter() { phase[0] = 'filter'; return true; }function doTransform() { phase[0] = 'transformer'; logger = Packages.org.apache.log4j.Logger.getLogger(phase[0]);
102:
103:
LINE NUMBER: 99
DETAILS: SyntaxError: Empty JSON string

I'm using Mirth 3.6.1.
I know a blank message is not valid JSON, but how should I handle both the GET and the POST?

Any help or suggestions would be appreciated.
Thanks
Leon
Attached Files
File Type: xml TestListener.xml (13.1 KB, 3 views)
File Type: xml TestChallenge.xml (16.3 KB, 2 views)

Last edited by leon; 08-08-2018 at 06:07 AM. Reason: Changed TCP to HTTP in title
Reply With Quote
  #2  
Old 08-08-2018, 04:30 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 530
agermano is on a distinguished road
Default

Use Raw for your types.

When you need to access a JSON body, do
Code:
msg = JSON.parse(msg);
Then continue as normal.
Reply With Quote
  #3  
Old 08-09-2018, 10:07 AM
leon leon is offline
Mirth Newb
 
Join Date: Jul 2013
Location: South Africa
Posts: 7
leon is on a distinguished road
Smile

Quote:
Originally Posted by agermano View Post
Use Raw for your types.

When you need to access a JSON body, do
Code:
msg = JSON.parse(msg);
Then continue as normal.
Thanks Agermano

I sounds like it should work! I will test it on Monday.
Reply With Quote
  #4  
Old 08-14-2018, 07:38 AM
leon leon is offline
Mirth Newb
 
Join Date: Jul 2013
Location: South Africa
Posts: 7
leon is on a distinguished road
Default

Thanks Agermano

It worked for me.
Reply With Quote
Reply

Tags
http get, http listener, http post, json, transformer error

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


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