Skip to main content

Pentaho Data Integration - Multi-part Form submission with file upload using the User Defined Java Class Step

I recently needed to use Pentaho Data Integration (PDI) to send a file to a server for processing using HTTP Post. I spent several hours trying to use the existing steps HTTP Post, HTTP Client & Rest Client but I couldn't get it to work. After some more research I came across the issue PDI-10120 - Support for Multi-part Form Submittal In Web Service Steps and I thought I was out of luck.

I previously wrote a small Java client for a similar use case and remembered the PDI has a step called User Defined Java Class (UDJC). After reading this great tutorial I created the following basic transaction. I have a dataset with the URL and the full file path and use the UDJC to make the HTTP call.

HTTP Post using User Defined Java Class

The Java class handles the actual HTTP Post. It uses 2 input variables, the URL (url) which is used for the call and the file name (longFileName). The HTTP call then contains the file (line 30) and the file name (line 31). I included some basic error handling based on the HTTP status code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.*;
import org.apache.http.impl.client.HttpClients;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
 Object[] r = getRow();
 
    if (r == null) {
        setOutputDone();
        return false;
    }

 r = createOutputRow(r, data.outputRowMeta.size());
 
  // Get the value from url
    String urlString = get(Fields.In, "url").getString(r);
 // Get the file name 
 String longFileNameString = get(Fields.In, "longFileName").getString(r);
 // Load the file
 File file = new File(longFileNameString);
 
 HttpPost post = new HttpPost(urlString);
 MultipartEntityBuilder entity = MultipartEntityBuilder.create();
 entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
 entity.addBinaryBody("file", file);
 entity.addTextBody("fileName", file.getName());
 post.setEntity(entity.build());

 HttpClient httpClient = HttpClients.createDefault();

 try{ 
  //Make HTTP Call 
  HttpResponse response = httpClient.execute(post);
  // Check if response code is as expected
  if (response.getStatusLine().getStatusCode() != 200) {
   putError(data.outputRowMeta, r, 1, "Error returned", "HTTP Status Code", "HTTP Status: " + response.getStatusLine().getStatusCode());
   return true;
  } 
  // Set value of HTTP Status, integer values need conversion
  get(Fields.Out, "http_status").setValue(r,  Long.valueOf(response.getStatusLine().getStatusCode()));
 } catch (Exception e) {
  // Set value of HTTP Status to -1 since HTTP Post caused exception
  get(Fields.Out, "http_status").setValue(r,  Long.valueOf(-1));
  return true;
 }

 // Send the row on to the next step.
    putRow(data.outputRowMeta, r);
 
 return true;
}

To get this example to work you will have to download the Apache HTTP Client and add the JAR files to the lib folder from PDI.

Click here to download the sample transformation. I hope it will be helpful for others, use at your own risk.

Comments

Anonymous said…
Can we send PDF as well?
dhakim said…
Hello, I am getting error on Line 28, Column 31: Cannot determine simple type name "MultipartEntityBuilder" although, I have copied "httpmime-4.5.10.jar" in my Pentaho DI Lib folder.
Ben said…
I just checked my lib folder, I added the following files. I dont' remember 100% if all of them were required for multi-part form submission but this version should work.
httpclient-4.3.3.jar
httpmime-4.3.3.jar
httpcore-4.3.2.jar
Anonymous said…
For some reason the transformation is not working on PDI 9. The transformation doesn't get any errors but the file is never sending.

The response field is also empty so i can't figure out what the error is.
Angel said…
Any luck resolving this issue?? You're right in that it only happens on PDI 9, but I also found that it happens on the PDI server instance. The issue is not present in the PDI client tools. I was digging through the logs and I found this error after copying the apache client jars:

ERROR (version 9.0.0.0-423, build 9.0.0.0-423 from 2020-01-31 04.53.04 by buildguy) : java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.http.client.methods.HttpEntityEnclosingRequestBase.setEntity(Lorg/apache/http/HttpEntity;)V" the class loader (instance of org/codehaus/janino/ByteArrayClassLoader) of the current class, Processor, and the class loader (instance of org/apache/catalina/loader/ParallelWebappClassLoader) for the method's defining class, org/apache/http/client/methods/HttpEntityEnclosingRequestBase, have different Class objects for the type org/apache/http/HttpEntity used in the signature
Anonymous said…
I haven't tried it myself yet with PDI 9 but the message " have different Class objects for the type org/apache/http/HttpEntity used in the signature" seems to indicate that PID includes some of the libraries in a different version causing this issue
Angel said…
It looks like PDI 9 includes httpclient and and httpcore in their WEB-INF/lib directory, so httpmime just needs to be deployed there as well instead of the main tomcat/lib directory.

This solved the issue for me

Popular posts from this blog

Products you don't expect to be 'Made in China' - Del Monte fruit cups

Since I moved to Canada back in March I have started to realize how many products are actually made in China. Back in Germany you could also buy lots of stuff from China but you mostly had the choice between German or Europe products and Chinese products. When I went to Food Basics in Oakville a couple weeks ago to get some apples I stood in front of a huge tray of Chinese apples! Aren't there enough apples in Ontario, Canada or the US? Even Mexico would probably be closer than China. Another day my wife bought Del Monte fruit cups in the grocery store. I checked the label when I was going to eat it and i decided to leave it in the fridge. First of all it is 'Made in China' (again I guess no other country in this world has fruit) and second it contains artificial flavor. How bad must the fruit inside be that you need artificial flavor (and does anybody in China controls how it is made)? For my part I'll check the labels more closely whenever I buy any kind of product

Open Source tool for Data cleansing and Master Data Management

Last weekend SQL Power released an improved version of SQL Power DQguru (formerly known as SQL Power MatchMaker), one of the few open source tools for data cleansing and master data management (MDM) available. Version 0.96 brings a new feature that allows you to run SQL Power DQguru from command line. This allows you to integrate it into batch scripts and your ETL jobs. As a BI consultant for SQL Power I have used SQL Power DQguru in different projects and it has made my job a lot easier. Some of the features I like the most are: Easy connection to any database with JDBC drivers, incl. SQL Server, Oracle, MySQL, Postgres Lets you create complex merge rules so your dependent data will always be updated when you merge records. You can combine over 25 steps to find possible duplicate data with a match rule, for example: Word Count Regular Expressions Substrings Retain certain characters Translate Words, you can create your own translation rules. You can preview how your data