No announcement yet.

Send data in body of HttpPost using javascript

  • Filter
  • Time
  • Show
Clear All
new posts

  • Send data in body of HttpPost using javascript

    I'm new to the mirth. Is there a way to send the data to the body of HttpPost request?

    I want to call a post rest service from the destination using javascript, but I couldn't find any way of sending data in request body. I've selected connector type as Javascript Writer and able to add the header using httpPost.addHeader("headerName", "headerValue"). Just like this is there a way to add parameters to the request body?

    I know we can do this using HttpSender connector type, but in my scenario, I've to call from javascript only. Below is sample code for calling rest service I'm using.

    var httpPost= new HttpPost(url);
    httpPost.addHeader("headerName", "headerValue");
    httpPost.addRequestBody("paramName","paramvalue");//Tried different ways, but not working
    var response = httpclient.execute(httpPost);

    Thanks in advance!
    Last edited by RamY; 04-03-2018, 12:24 PM.

  • #2
    You need Data Streams to send the data out and receive the response.
    Below piece of code is by using package to do what you are intending to do using the httpUtils library.

         var url = new;
        // Open connection to url
        var conn = url.openConnection();
        // Send request
        var outStream = conn.getOutputStream();
        var outWriter = new;
        //Capture Response
        var respCode = conn.getResponseCode();
        var inputStream = conn.getInputStream();
        var streamReader = new;
        var respStream = new;
        var buffer = '';
        var line = null;
        while ((line = respStream.readLine()) != null) {
            buffer = buffer + line;

    I am curious to know how is your case any different and why you cannot use regular HTTP Sender.
    HL7v2.7 Certified Control Specialist!


    • #3
      We dont use filestreams to add an item to the Body of a POST. We use setEntity(). If you do not have a large amount of data for the body, you may not need to stream it:

      //build client
      var client = new HttpClientBuilder.create().build();
      //create a reuest for of POST for the submission
      var httpPost = new HttpPost(submissionURL);
      //add some headers if needed
      httpPost.addHeader("Content-Type", 'application/json');
      httpPost.addHeader("Accept", 'application/json');
      //create entity
      entity = new StringEntity(TEXTFORBODY); 
      //add entity to BODY
      //execute post and get respose			
      var resp = client.execute(httpPost);
      //parse response
      var rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
      //read in the response
      var res = new String()
      var line = "";
      while ((line = rd.readLine()) != null) {
      	res += line;
      //do something with results
      var json = JSON.parse(res);


      • #4
        Thanks for your quick reply!
        @Siddharth We've multiple stored procedure calls from one Database Writer destination javascript before and now, we're replacing with rest services those calls. Now I have just created javascript writer as a destination and calling rest services. Anyways, I made a custom java class which handles all the requests and deployed that jar file into the mirth. Seems like working fine.


        • #5
          It would work fine, but it creates problems when tracking. For instance you would need to code around error conditions, and reflect them to the channel dashboard.
          HL7v2.7 Certified Control Specialist!