I'm having trouble sending a simple PUT request to nifi, basically I want to start a nifi processor with java code.
Error message - Server returned HTTP response code: 405 for URL: http://localhost:8080/nifi-api/processors/
What steps should I take to resolve this 405 issue? The nifi api website provides a high level documentation on rest api but there is no guide on how to make a proper PUT request to NiFi with Java.
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPut();
}
// HTTP PUT request
private void sendPut() throws Exception {
String url = "http://localhost:8080/nifi-api/processors/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("PUT");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("text/javascript", "*/*");
String urlParameters = "{\r\n" +
" \"status\": {\r\n" +
" \"runStatus\": \"RUNNING\"\r\n" +
" },\r\n" +
" \"component\": {\r\n" +
" \"state\": \"RUNNING\",\r\n" +
" \"id\": \"749b3133-0162-1000-9acc-f384457b160c\"\r\n" +
" },\r\n" +
" \"id\": \"749b3133-0162-1000-9acc-f384457b160c\",\r\n" +
" \"revisions\": {\r\n" +
" \"version\": 1,\r\n" +
" \"clientId\": \"550c4b84-0165-1000-37be-724ed17b5329\"\r\n" +
"}\r\n" +
" ";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}