Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Uploading a template in apache NiFi using Rest API in Java

avatar
Rising Star

I am trying to create a REST API call to import a template into my NiFi UI post which Instantiate the same. Below is the code which I tried,

String pathname = "D:\\Users\\bramasam\\Downloads\\BalaBackUp.xml";
String restString = "http://localhost:8080/nifi-api/process-groups/f80896d4-c71f-3395-d527-8c6bd69f44d0/templates/upload";
HttpPost httpPost = new HttpPost(restString);
File fileObj = new File(pathname);
httpPost.addHeader("Content-type", "multipart/form-data");
FileEntity fileEntity = new FileEntity(fileObj, ContentType.MULTIPART_FORM_DATA);
httpPost.setEntity(fileEntity);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);
StatusLine status = response.getStatusLine();
System.out.println(status.getStatusCode());

I am getting a response code of 500 and with the below response

HttpResponseProxy{HTTP/1.1 500 Internal Server Error [Date: Thu, 28 Sep 2017 09:43:28 GMT, X-Frame-Options: SAMEORIGIN, Content-Type: text/plain, Transfer-Encoding: chunked, Server: Jetty(9.4.3.v20170317)] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}} 

Below is the {id} from the BalaBackUp.xml file which I am trying to import from

<?xml version="1.0" ?>
<template encoding-version="1.1">
  <description></description>
  <groupId>bd5dba8b-015d-1000-1fd5-450ede38b7a5</groupId>
  <name>BalaBackUp</name>
  <snippet>
    <processGroups>
      <id>f80896d4-c71f-3395-0000-000000000000</id>
      <parentGroupId>29a5776d-9728-3fee-0000-000000000000</parentGroupId>
      <position>
        <x>0.0</x>
        <y>0.0</y>
      </position>
      <comments></comments>
      <contents>
        <connections>
          <id>c0d0e26d-5ee2-3d60-0000-000000000000</id>
          <parentGroupId>f80896d4-c71f-3395-0000-000000000000</parentGroupId>
          <backPressureDataSizeThreshold>1 GB</backPressureDataSizeThreshold>
          <backPressureObjectThreshold>10000</backPressureObjectThreshold>
          <destination>
            <groupId>f80896d4-c71f-3395-0000-000000000000</groupId>
            <id>1f9e926a-71fc-356f-0000-000000000000</id>
            <type>PROCESSOR</type>

can you please help me on what I am missing?

3 REPLIES 3

avatar
Rising Star

@Andrew Grande can you please help on this?

avatar
New Contributor

This worked well for me:

I think you need to add a part on the MultipartEntity containing the FileBody which name should be "template"

val templateFile = new File(templatePath)<br>if(!templateFile.exists() || !templateFile.isFile) None<br>else{<br>  val multipartEntity = MultipartEntityBuilder.create().addPart("template", new FileBody(templateFile))<br>      .setContentType(ContentType.MULTIPART_FORM_DATA).build()<br><br>  val post = new HttpPost(nifiBaseUrl.concat(s"/process-groups/${processGroupId.getOrElse("root")}/templates/upload"))<br><br>  post.setEntity(multipartEntity)<br>  Some(post)

avatar
Rising Star
@Jorge moyano

I tried the below code as you suggested but still facing the same problem

File fileObj = new File(pathname);
FileBody fileBody = new FileBody(fileObj, ContentType.MULTIPART_FORM_DATA);
HttpEntity multiPart = MultipartEntityBuilder.create().addPart("template", fileBody).build();

HttpPost httpPost = new HttpPost("http://localhost:8080/nifi-api/process-groups/root/templates/upload");
httpPost.addHeader("Content-type", "multipart/form-data");
httpPost.setEntity(multiPart);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);

Response

HttpResponseProxy{HTTP/1.1 500 Internal Server Error [Date: Sat, 30 Sep 2017 04:24:37 GMT, X-Frame-Options: SAMEORIGIN, Content-Type: text/plain, Transfer-Encoding: chunked, Server: Jetty(9.4.3.v20170317)] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}}