0
votes

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());

    }

}
1

1 Answers

4
votes

A 405 means the HTTP method, in this case PUT, is not supported by the URL that was used. This is because /processors only supports a GET request to retrieve available processors. In order to update a processor the URL would be /processors/{processorId} where the processorId was replaced with the given processor id that you want to update.

In general the best way to learn NiFi's REST API is to open a tool like Chrome's Dev tools, and then look at the network to see the requests that are made while you perform the desired action from the UI. These would show you the URLs, parameters, and request/response bodies.

UPDATE

Here is what the UI sends when starting an UpdateAttribute processor which makes a PUT to http://localhost:8080/nifi-api/processors/5817008a-0165-1000-0bf3-2c44fde14c37

{
  "revision":{
    "clientId":"61a9ab71-0165-1000-777c-c7538789246c",
    "version":0
    },
    "component":{
      "id":"5817008a-0165-1000-0bf3-2c44fde14c37",
      "state":"RUNNING"
    },
    "disconnectedNodeAcknowledged":false
}

Here is the request shown in Chrome Dev Tools: enter image description here