Using Apache HttpClient to HTTP GET a resource and stream it directly to a file - rbeckman-nextgen/test-mc GitHub Wiki

  1. Mirth Connect
  2. Home
  3. Examples and Tutorials

Mirth Connect : Using Apache HttpClient to HTTP GET a resource and stream it directly to a file

Created by Roy Feague, last modified on Mar 19, 2010

Here's a javascript transformer step I created to get a file via HTTP GET using Apache HttpClient v4.0.1.
It shows how to:

  • make calls to Java using importPackage instead of using the fully-qualified name on every object reference
  • take advantage of the Entity functions HttpClient 4.0 to stream directly to your target file

In order for this to work, you must first download the HttpClient jar files WITH DEPENDENCIES and put them into the your C:\Program Files\Mirth\lib\custom folder. (See http://hc.apache.org/downloads.cgi)

importPackage(Packages.org.apache.http.client);
importPackage(Packages.org.apache.http.client.methods);
importPackage(Packages.org.apache.http.impl.client);

var httpclient = new DefaultHttpClient();

var httpget = new HttpGet('http://myserver/mypath');
var response = httpclient.execute(httpget);
var entity = response.getEntity();
if (entity != null) {
    var fos = new java.io.FileOutputStream('c:\\temp\\myfile.ext');
    entity.writeTo(fos);
    fos.close();
}

Comments:

Can this method be used to connect to a secure URL requiring a username and password?

Posted by [email protected] at Apr 29, 2011 09:00

Document generated by Confluence on Nov 11, 2019 08:40

Atlassian

⚠️ **GitHub.com Fallback** ⚠️