Hi,
(Pardon my ignorance) I would like to know how to get the JSON string from ResultsReaderJson or any other API. We need to send the output in JSON format to a URL.
Here is what I plan to achieve. To get the output from Splunk in the form of JSON and feed this input to another RESTful service, say ABC.
For ABC, if we directly give through curl in the following format, it works.
curl -d '{ "index": "myindex", "data": 2340 ' http://localhost/test/mytest
I have tried to read INputstream - but it doesnt give the values.
Args outputArgs = new Args();
outputArgs.put("output_mode","json");
InputStream results= jobSavedSearch.getResults(outputArgs);
ResultsReaderJson resultsReader = new ResultsReaderJson(results);
InputStreamReader resultsStreamReader = new InputStreamReader(results, "UTF8");
OutputStreamWriter writer = new OutputStreamWriter(System.out);
int size = 1024;
int count = 0;
char[] buffer = new char[size];
try
{
while (true) {
count = resultsStreamReader.read(buffer);
System.out.println("countis "+ count);
if (count == -1) break;
writer.write(buffer, 0, count);
}
writer.write("\n");
writer.close();
resultsStreamReader.close();
}
Nothing gets printed and nothing in buffer - so not able to get the data in the form of JSON from splunk.
I have tried using Java the data shown in the the curl example above and it works ( below is code if need be)
String baseURL = "http://localhost/test/mytest";
String urlString = baseURL;
System.out.println("Request: " + urlString);
String dataString =
"{ \"index\": \"myindex\", \"data\": 2340 }";
System.out.println("Data: " + dataString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "application/json");
conn.setReadTimeout(10000); // time out in 10 seconds
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream output =
new DataOutputStream(conn.getOutputStream ());
output.write(dataString.getBytes());
output.flush ();
output.close ();
//
... View more