I just want to start Nifi processor through REST API java code, i am able to invoke HTTP connection and able to see play button on processors but flow is not happening? and i have multiple processes group in which my first processor is GETSplunk Template which is in cron driven ,manual start is good and fine and when i start through API flow is not working, and changed to Timer schedule it is showing error for SQL template ,can any one bumped with this issue, please suggest me.
sample API code is.
String url = "http://hostname:8080/nifi-api/flow/process-groups/{id};
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Setting basic put request
con.setDoOutput(true);
con.setRequestMethod("PUT");
con.setRequestProperty("Content-Type","application/json");
String putJsonData = "{\r\n" +
"\"component\":{\r\n" +
"\"id\":\"<processor-group id>\",\r\n " +"\"state\":\"RUNNING\"\r\n" + "}";
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(putJsonData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + putJsonData);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader( new
InputStreamReader(con.getInputStream())); String output; StringBuffer
response = new StringBuffer();
while ((output = in.readLine()) != null) { response.append(output); }
in.close();
//printing result from response
System.out.println(response.toString());
}
#Sravya99 When I need to trigger flow from outside of Nifi, I often create a NiFi API interface listening on a port via HandleHttpRequest and HandleHttpResponse. This can be very simple request, or very complicated with ssl, access and authorization, etc. It should serve fine for your purpose, leaving your flow always on, and initiating the triggered responses using the HandleHttpRequest.
Related
I'm trying to login to a portal. It works using Postman. When I try the same request using plain Java or OkHttp the login fails and I will be redirected to the login page.
HttpUrl.Builder httpBuilder = HttpUrl.parse("https://test58.cashctrl.com/auth/login.html").newBuilder();
httpBuilder.addQueryParameter("JMCF_AUTH_EMAIL", "email");
httpBuilder.addQueryParameter("JMCF_AUTH_PASSWORD", "password");
Request request = new Request.Builder()
.url(httpBuilder.build())
.get()
.build();
I know the Url looks weird but it works this way using Postman or even simply use a browser.
Alternative with plain Java, which I tried:
Map<String, String> parameters = new HashMap<>();
parameters.put(PARAM_EMAIL, EMAIL);
parameters.put(PARAM_PASSWORD, PASSWORD);
URL url = new URL(LOGIN_URL + "?" + ParameterStringBuilder.getParamsString(parameters));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setInstanceFollowRedirects(true);
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine + "\n");
}
in.close();
con.disconnect();
System.out.println(status);
System.out.println(content.toString());
Postman must be doing something special or also a browser which I don't see.
I had the same issue, I got to know that Postman has "code" feature. Below the send button you can see the code option it will generate the code for you. There is a list of language to choose from and java is one of them. Do check that out. Also you must be missing the cookie, see the temporary headers in Postman add all in your code and do include the cookie one.
Thanks I hope it helps.
I'm trying to create a client server application and I'm currently stuck. I have this java code on my client.
HttpURLConnection connection;
connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.setRequestProperty("Accept","application/json: charset=UTF-8");
connection.setRequestMethod("POST");
System.out.println("The request method on client end is " + connection.getRequestMethod());
System.out.println("Server response to connection " + connection.getResponseMessage());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String buffer;
StringBuilder stringBuilder = new StringBuilder();
while ((buffer = reader.readLine()) != null) {
stringBuilder.append(buffer);
}
System.out.println("The request methond on server end is " + stringBuilder.toString());
And this is the simple test php code on the server.
<?php echo $_SERVER['REQUEST_METHOD']; ?>
Whenever I run this test java program and try to connect I get the same output.
The request method on client end is POST
Server response to connection OK
The request methond on server end is GET
The php script always echoes back that I'm sending a GET request even though
my java code states that use a POST. I have tried connecting to the script through postman using different request methods and it's all fine, so the problem must be in the Java code. Any insight would be greatly appreciated.
I am not having any experience of php but it seems like it is looking for content-length header which will not be send in your case so it is treating this as GET request.
HttpURLConnection connection;
connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.setRequestProperty("Accept","application/json: charset=UTF-8");
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(0);
System.out.println("The request method on client end is " + connection.getRequestMethod());
System.out.println("Server response to connection " + connection.getResponseMessage());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String buffer;
StringBuilder stringBuilder = new StringBuilder();
while ((buffer = reader.readLine()) != null) {
stringBuilder.append(buffer);
}
System.out.println("The request methond on server end is " + stringBuilder.toString());
This will set content length 0 as you are not sending any content.
I have an issue regarding OData querying with an Java Client.
If I use Postman, everything works as expected and I'm receiving a response from the web service with the metadata. But in my Java Client, which runs not on the SCP / HCP I'm receiving "400-Bad Request". I used the original Olingo libary.
I only used the $metadata Parameter, so there is no filter value or something else.
public void sendGet(String user, String password, String url) throws IOException, URISyntaxException {
// String userPassword = user + ":" + password;
// String encoding = Base64.encodeBase64String(userPassword.getBytes("UTF-8"));
URL obj = new URL(url);
URL urlToEncode = new URL(url);
URI uri = new URI(urlToEncode.getProtocol(), urlToEncode.getUserInfo(), urlToEncode.getHost(), urlToEncode.getPort(), urlToEncode.getPath(), urlToEncode.getQuery(), urlToEncode.getRef());
// open Connection
HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();
// Basis Authentifizierung
con.setRequestProperty("Authorization", "Basic " + user);
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("Content-Type", "application/xml");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
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);
response.append("\n");
}
in.close();
// print result
System.out.println(response.toString());
// Schließt eine Vorhandene Verbindung
con.disconnect();
in User is already the encoded value. by manipulating this one, i'm receiving an authorization error, so already tested.
May somebody can help me in that case :)
Thanks in advance.
Tim
So I solved it by myself.
i added the statement con.setRequestProperty("Accept", "application/xml"); and it works fo me.
Maybe it could help somebody else.
I'm trying to send a PUT request from a Java app to a server. I successfully send GET, POST and DELETE requests but the PUT one won't succeed (I'm getting a 401 Error with the code below, 405 Error with an other code using the HttpPut of the apache package).
I'm using java.net.HttpURLConnection, here is a small region of my code :
URL obj = new URL(urlPost);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add request header
con.setRequestMethod(typeRequest); //typeRequest = PUT
String credentials = adminOC + ":" + pwdOC;
String encoding = Base64.encode(credentials.getBytes("UTF-8"));
con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
if (!typeRequest.equals("GET")){
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(postParam);
wr.flush();
}
}
if (con.getResponseCode() == 200){
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response += inputLine;
}
}
}
I tried sending my PUT parameters the "POST" way and also directly in the URL.
It seems to be an error from my Java code and not from the server because I tried to do the PUT request with cURL and it worked.
Thanks for reading, I hope you will be able to give me some hints to debug the problem.
What is missing in your code is con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
I want to set a successful request to Neteller, I am trying to get an access token using the code from the Neteller documentation. However, it consistently fails with with the following exception:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials
Here's the code (again, from the Neteller documentation):
String testUrl = " https://test.api.neteller.com";
String secureUrl = "https://api.neteller.com";
String url = testUrl;
if("live".equals(configBean.get("environment"))){
url = secureUrl;
}
url += "/v1/oauth2/token?grant_type=client_credentials";
String xml = "grant_type=client_credentials?grant_type=client_credentials";
xml = "";
String test = Base64.encodeBytes((accountID + ":" + secureID).getBytes());
try {
URL urls = new URL ("https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials");
HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Bearer " + test);
connection.setRequestProperty ("Content-Type", "application/json");
connection.setRequestProperty ("Cache-Control", "no-cache");
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String accessToken = "";
} catch(Exception e) {
e.printStackTrace();
}
Why is my implementation failing here?
There is nothing wrong with your code. The problem is that you are trying use a regular member account for the API integration, where you need to be using a merchant account for that. Below are the steps you will need to complete in order to get it to work:
You need to get a test merchant account (http://www.neteller.com/business/contact-sales/). Registering on www.neteller.com creates a regular member account, which cannot receive payments via the API.
Once you have a test merchant account, you will need to white-list the IP address from which you will be making requests to the API. (pg. 31 of the manual).
Then, you will need to add an application to it (pg. 32 of the manual).
Once you have added the application, use the "client ID" and "client secret" in the Authorization header - just like you do now, base64 encoded values, separated with colon (:).