Balaji
Balaji

Reputation: 172

Pentaho - upload file using API

I need to upload a file using an API.

I tried REST CLIENT and didn't find any options. Tried with HTTP POST and that responded with 415.

Please suggest how to accomplish this

Upvotes: 1

Views: 3871

Answers (3)

mesompi
mesompi

Reputation: 699

This solution uses only standard classes of jre 7. Add a step Modified Java Script Value in your transformation. You will have to add two columns in the flow: URL_FORM_POST_MULTIPART_COLUMN and FILE_URL_COLUMN, you can add as many files as you want, you will just have to call outputStreamToRequestBody.write more times.

    try
    {
        //in this step you will need to add two columns from the previous flow -> URL_FORM_POST_MULTIPART_COLUMN, FILE_URL_COLUMN 
        var serverUrl =  new java.net.URL(URL_FORM_POST_MULTIPART_COLUMN);

        var boundaryString = "999aaa000zzz09za";
        var openBoundary  = java.lang.String.format("\n\n--%s\nContent-Disposition: form-data\nContent-Type: text/xml\n\n" , boundaryString);
        var closeBoundary = java.lang.String.format("\n\n--%s--\n", boundaryString);

        // var netIPSocketAddress = java.net.InetSocketAddress("127.0.0.1", 8888);
        // var proxy = java.net.Proxy(java.net.Proxy.Type.HTTP , netIPSocketAddress);
        // var urlConnection = serverUrl.openConnection(proxy);
        var urlConnection = serverUrl.openConnection();

        urlConnection.setDoOutput(true); // Indicate that we want to write to the HTTP request body
        urlConnection.setRequestMethod("POST");
        //urlConnection.addRequestProperty("Authorization", "Basic " + Authorization);
        urlConnection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);


        var outputStreamToRequestBody = urlConnection.getOutputStream();

        outputStreamToRequestBody.write(openBoundary.getBytes(java.nio.charset.StandardCharsets.UTF_8));
        outputStreamToRequestBody.write(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(FILE_URL_COLUMN)));
        outputStreamToRequestBody.write(closeBoundary.getBytes(java.nio.charset.StandardCharsets.UTF_8));

        outputStreamToRequestBody.flush();


        var httpResponseReader = new java.io.BufferedReader(new java.io.InputStreamReader(urlConnection.getInputStream()));

        var lineRead = "";
        var finalText = "";
        while((lineRead = httpResponseReader.readLine()) != null) {
            finalText += lineRead;
        }

        var status = urlConnection.getResponseCode();
        var result = finalText;
        var time   = new Date();

    }
    catch(e)
    {
        Alert(e);
    }

Upvotes: 1

Balaji
Balaji

Reputation: 172

I solved this by using the solution from http://www.dietz-solutions.com/2017/06/pentaho-data-integration-multi-part.html

Thanks Ben.

He's written a Java class for Multi-part Form submission. I extendd by adding a header for Authorization...

Upvotes: 0

nsousa
nsousa

Reputation: 4544

Error 415 is “Unsupported media type”. You may need to change the media type of the request or check whether that type of file us accepted by the remote server.

https://en.m.wikipedia.org/wiki/List_of_HTTP_status_codes

Upvotes: 1

Related Questions