1

I'm trying to run a curl command that consumes a webservice I'm running:

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(curl);
    process.waitFor();
} catch (Exception e) {
    e.printStackTrace();
}

If I copy the log message from the Sysout and paste it on my terminal, it runs as expected, but when I run the java code, it seems to return an html page from the proxy not finding the service.

Do I have to add something else for it to run from java?

Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23
  • Possible duplicate of [How to use cURL in Java?](http://stackoverflow.com/questions/2586975/how-to-use-curl-in-java) – pczeus Apr 22 '16 at 00:24

1 Answers1

1

The problem is that you don't have it read the output. I modified your code so it should work. I'm not 100% sure because I can't test it properly for some reason.

EDIT - It works, I was just executing the wrong command! This should work for you.

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(curl);
    process.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
    StringBuilder sb = new StringBuilder(); //What will hold the entire console output
    String line = ""; //What will hold the text for a line of the output
    while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
        sb.append(line + "\n"); //Append the line to the StringBuilder
    }
    System.out.println(sb); //Print out the full output
} catch (Exception e) {
    e.printStackTrace();
}

EDIT - New code utilising ProcessBuilder instead of Runtime

String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", curl);
builder.redirectErrorStream(true);
Process p = builder.start();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
int linenum = 0;
while (true) {
    linenum++;
    line = r.readLine();
    if (line == null) {
        break;
    }
    sb.append(line);
}
System.out.println(sb);
Butterscotch
  • 100
  • 11
  • Well thanks to you at last now I get some feedback from the curl, it seems to be failing because of the proxy, but if I run the command myself in a terminal, it works, so I still don't fully understand what is wrong. – Artemio Ramirez Apr 22 '16 at 01:04
  • Maybe the Java program requires more permissions? It's possible that might be the problem. Try running it with `sudo`? – Butterscotch Apr 22 '16 at 01:10
  • Hmm, I've got an idea if that previous one doesn't work. Try using `String curl = "/bin/bash -c curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";`. That will run the command using the terminal – Butterscotch Apr 22 '16 at 01:14
  • doesn't work and I also stop getting any feedback if I add /bin/bash -c to it – Artemio Ramirez Apr 22 '16 at 01:17
  • Hmmm... Try doing this: `String curl = "sh curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";` – Butterscotch Apr 22 '16 at 01:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109859/discussion-between-dankrushen-and-artemio-ramirez). – Butterscotch Apr 22 '16 at 01:23