0

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 siteUrl = "localhost";
String portNumber = "8080";
String pId = "f80896d4-c71f-3395-d527-8c6bd69f44d0";
String pathname = "D:\\Users\\bramasam\\Downloads\\BalaBackUp.xml";
String restString = "http://" + siteUrl + ":" + portNumber + "/nifi-api/process-groups/" + pId + "/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());

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>

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]}}

can you please help me on what I am missing?

Andy
  • 13,916
  • 1
  • 36
  • 78
Gladiator
  • 354
  • 3
  • 19

2 Answers2

2

HttpClient should be built using DefaultHttpClient() and not HttpClientBuilder. Below is the code snippet. Also, you have to addPart the name 'template' for nifi to recognize it as a template

File file = new File(pathname);

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(restString);

FileBody uploadFilePart = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("template", uploadFilePart);
httpPost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httpPost);

Reference URL below for MultipartEntity() POST request How can I make a multipart/form-data POST request using Java?

Gladiator
  • 354
  • 3
  • 19
0

You need to examine the error in the logs/nifi-app.log file which will explain what the HTTP 500 exception is. Try using your browser's developer tools panel to examine the underlying network requests when you upload a template through the Apache NiFi UI, as those will be the same requests necessary to perform this programmatically.

I'm assuming the copied template XML is incomplete, as that is not a valid template and will definitely cause an internal server exception if it is provided as-is.

Andy
  • 13,916
  • 1
  • 36
  • 78
  • I verified the XML file and it is complete(And I was able to upload it manually). Also, I checked nifi-app.log, but could not find any error for HTTP 500 Exception. Is the way I am building the XML file into "multipart/form-data" right. – Gladiator Oct 05 '17 at 09:12
  • Maybe it is SO, but your XML file shows only 24 lines and it does not close any of the opening tags. What is visible to me is not valid XML. Any error in the internal NiFi framework or a processor execution will cause an error stacktrace to be printed to the nifi-app log. I am not sure why you are not able to see the error causing the HTTP 500 error. – Andy Oct 05 '17 at 09:16
  • Sorry for the miss in communication. I have pasted only the XML sample to show from where I got the {id} field. It is a complete XML. – Gladiator Oct 05 '17 at 09:59
  • I have another suggestion in Hortornworks community in the below link. But, that too isnt working https://community.hortonworks.com/questions/139475/uploading-a-template-in-apache-nifi-using-rest-api.html?childToView=139588#answer-139588 – Gladiator Oct 05 '17 at 11:07