Issue
I am calling curl with a java Process to send json text to an API. I call this code in a loop and all the updates work, except the first one, which fails with exit code 35. I believe the error code comes from curl and is a TLS/SSL connect error.
This is the heart of the method that gets repeatedly called. Again, only the first update fails, all subsequent updates succeeed. Maybe there is some overhead with establishing the initial File or ProcessBuilder which is then in place for the subsequent calls?
File f = new File("c:\\temp\\json.txt");
String str = "{\"batch_fields\":{\"Batch Number\":\""+num+"\"}}";
try(BufferedWriter writer = new BufferedWriter(new FileWriter(f))) {
writer.write(str);
}
StringBuilder sb = new StringBuilder();
StringBuilder command = new StringBuilder("c:\\Program Files\\curl\\bin\\curl -H \"X-Token: "+BatchUtils.API_TOKEN_READ_WRITE+"\"");
command.append(" -X PUT -H \"Content-Type: application/json\" ");
command.append(" -d \"@c:\\temp\\json.txt\" ");
command.append("https://blah/blah/batches/"+batchId+"\"");
ProcessBuilder processBuilder = new ProcessBuilder(command.toString().split(" "));
Process process = processBuilder.start();
InputStream is = process.getInputStream();
if(is == null || is.read() == -1) {
is = process.getErrorStream();
sb.append(" Failed updateBatchNumber: " + is.toString());
}
else
sb.append(" Updated batch number";
int exitCode = process.waitFor();
sb.append(". exit code: " + exitCode);
process.destroy();
Solution
Try simplifying to the below first. Of course, the json creation is less than proper but I've left it as is for now. You can add in more when you get proper error output, which the following should give you:
String str = "{\"batch_fields\":{\"Batch Number\":\"" + num + "\"}}";
Files.writeString(Path.of("C:/temp/json.txt"), str);
List<String> command = new ArrayList<>();
command.add("c:\\Program Files\\curl\\bin\\curl");
command.addAll(List.of("-H", "X-Token: " + BatchUtils.API_TOKEN_READ_WRITE));
command.addAll(List.of("-X", "PUT"));
command.addAll(List.of("-H", "Content-Type: application/json"));
command.addAll(List.of("-d", "@c:\\temp\\json.txt"));
command.add("https://blah/blah/batches/" + batchId);
new ProcessBuilder(command).inheritIO().start();
Answered By - g00se Answer Checked By - Dawn Plyler (WPSolving Volunteer)