Announcement

Collapse
No announcement yet.

TCP Listener to handle both GET verification without body and POST with JSON body

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP 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
    Last edited by leon; 08-08-2018, 05:07 AM. Reason: Changed TCP to HTTP in title

  • #2
    Use Raw for your types.

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

    Comment


    • #3
      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.

      Comment


      • #4
        Thanks Agermano

        It worked for me.

        Comment

        Working...
        X