Announcement

Collapse
No announcement yet.

"GET" RESTful endpoint

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

  • "GET" RESTful endpoint

    Does anyone have a working example of a channel, although based on what I'm reading it looks like I'll need 2 channels, of using a "GET" to pull results from a RESTful endpoint?

    I need to do this in the source and send the returned JSON back via TCP/IP as well as send the RESTful source confirmation I received the result.

  • #2
    Actually you can do it in one channel keeping the destination in the same chain. Create two destinations and for the first destination use HTTP Sender. Forward the response from that destination and for the second destination use TCP sender.

    Comment


    • #3
      Any idea on how I can check the endpoint every say minute checking if it has anything? I was thinking if I make the source as a JavaScript Reader and set the cron to the attached. Just assigning a variable in the JavaScript as sort of a "dummy" source to poll the endpoint. It doesnt seem to be working though as I think I need something to go through the source to get to the destination HTTP Sender using "GET".
      Attached Files

      Comment


      • #4
        The cron schedule will work, but you could also just use the default "Interval" polling schedule and set it to 1 minute.

        Your javascript reader must return something in order to generate a message that will trigger your destination. You may want to read the Javascript Reader section on page 327 of the user guide.

        Comment


        • #5
          Thanks! I'll have a look.

          Comment


          • #6
            That worked!

            Comment


            • #7
              Now since that way triggers a message at every interval to hit the "GET" on the HTTP listener on the destination. It will likely chew up a lot of space. If I changed the source so it uses a "GET" in JavaScript will that only send a message if it "GET"s a result from the endpoint and will still hit at every interval to let us know the channel is still "Alive"? I'm trying to use it as a heartbeat as well as pulling results from the endpoint when available. I found this other post as the XMLHttpRequest does not work in Mirth..

              https://www.mirthcorp.com/community/...d.php?t=216767

              Code:
              function getJSON(){
              	var URLstring = 'https://postman-echo.com/get?foo1=x';
                  
              	// Create url object
                  var url = new java.net.URL(URLstring);
              	
                  // Open connection to url
                  var conn = url.openConnection();
                  
              	//Set Headers
              //    conn.setDoOutput(true);
                  conn.setDoInput(true);
              	conn.setRequestMethod("GET");
              	conn.addRequestProperty("r",+ "json");
              	conn.addRequestProperty("Accept", "application/json");
              	
              	//Capture Response
                  var respCode = conn.getResponseCode();
              	var inputStream = conn.getInputStream();
                  var streamReader = new java.io.InputStreamReader(inputStream);
                  var respStream = new java.io.BufferedReader(streamReader);
                  var buffer = '';
                  var line = null;
                  while ((line = respStream.readLine()) != null) {
                      buffer = buffer + line;
                  }
                  respStream.close();
                  // This will be an object representation of this reponse JSON string
              	return (JSON.stringify(buffer))  
              }
              but can't get anything to return. Can anyone see what I may be missing?
              Last edited by mrando; 04-10-2020, 10:03 AM.

              Comment


              • #8
                Not sure if that actually saves space. But nice to see that code .
                Mirth 3.8.0 / PostgreSQL 11 / Ubuntu 18.04
                Diridium Technologies, Inc.
                https://diridium.com

                Comment


                • #9
                  Well I would only generate a message if something is returned as opposed to on each interval no matter what. I can't get that code to return the JSON though. What am I missing?

                  Comment


                  • #10
                    Your code works fine for me.

                    I removed your function tags and added return (JSON.parse(JSON.stringify(buffer))) to pretty the JSON, but seems to be fine.

                    If you are using your function I assume you are actually calling it somewhere.
                    Mirth 3.8.0 / PostgreSQL 11 / Ubuntu 18.04
                    Diridium Technologies, Inc.
                    https://diridium.com

                    Comment


                    • #11
                      Can I do it right from the Javascript reader source? That is how I have it setup which must not be correct. I'm fairly new to all of this so bare with me please.
                      Attached Files

                      Comment


                      • #12
                        Note bottom line, you need to return something.

                        Code:
                        function x() {
                        	var URLstring = 'https://postman-echo.com/get?foo1=x';
                            
                        	// Create url object
                            var url = new java.net.URL(URLstring);
                        	
                            // Open connection to url
                            var conn = url.openConnection();
                            
                        	//Set Headers
                        //    conn.setDoOutput(true);
                            conn.setDoInput(true);
                        	conn.setRequestMethod("GET");
                        	conn.addRequestProperty("r",+ "json");
                        	conn.addRequestProperty("Accept", "application/json");
                        	
                        	//Capture Response
                            var respCode = conn.getResponseCode();
                        	var inputStream = conn.getInputStream();
                            var streamReader = new java.io.InputStreamReader(inputStream);
                            var respStream = new java.io.BufferedReader(streamReader);
                            var buffer = '';
                            var line = null;
                            while ((line = respStream.readLine()) != null) {
                                buffer = buffer + line;
                            }
                            respStream.close();
                            // This will be an object representation of this reponse JSON string
                            return (JSON.parse(JSON.stringify(buffer)))  
                        }
                        return x();
                        Mirth 3.8.0 / PostgreSQL 11 / Ubuntu 18.04
                        Diridium Technologies, Inc.
                        https://diridium.com

                        Comment


                        • #13
                          BTW, you need to add error handling there. What if the endpoint is down?
                          Mirth 3.8.0 / PostgreSQL 11 / Ubuntu 18.04
                          Diridium Technologies, Inc.
                          https://diridium.com

                          Comment


                          • #14
                            You were able to get that to work? I have almost the exact same code just call it something different. I'm trying to pull every X amount of minutes.(I have it set for seconds for testing purposes.) The endpoint will know there is a connection issue if it doesn't get hit in the amount of time between intervals. I attached my setup on the source. I must be missing something.
                            Attached Files

                            Comment


                            • #15
                              Channel export

                              for_mrando.xml
                              Mirth 3.8.0 / PostgreSQL 11 / Ubuntu 18.04
                              Diridium Technologies, Inc.
                              https://diridium.com

                              Comment

                              Working...
                              X