web stats
Send data in body of HttpPost using javascript - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2018, 12:08 PM
RamY RamY is offline
What's HL7?
 
Join Date: Mar 2018
Posts: 2
RamY is on a distinguished road
Question 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 at 12:24 PM.
Reply With Quote
  #2  
Old 04-04-2018, 01:17 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 832
siddharth is on a distinguished road
Default

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

Code:
     var url = new java.net.URL(URLstring);

    // Open connection to url
    var conn = url.openConnection();
    // Send request
    var outStream = conn.getOutputStream();
    var outWriter = new java.io.OutputStreamWriter(outStream);
    outWriter.write(data);
    outWriter.close();

    //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();

I am curious to know how is your case any different and why you cannot use regular HTTP Sender.
__________________
HL7v2.7 Certified Control Specialist!
Reply With Quote
  #3  
Old 04-04-2018, 06:03 AM
BCMirthUser BCMirthUser is offline
OBX.2 Kenobi
 
Join Date: May 2015
Posts: 93
BCMirthUser is on a distinguished road
Default

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:

Code:
//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
httpPost.setEntity(entity);

//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);
Reply With Quote
  #4  
Old 04-04-2018, 09:09 AM
RamY RamY is offline
What's HL7?
 
Join Date: Mar 2018
Posts: 2
RamY is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 04-05-2018, 02:26 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 832
siddharth is on a distinguished road
Default

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!
Reply With Quote
Reply

Tags
httppost, javascript, rest api

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 05:54 AM.


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