2

I am trying to execute a curl command (which works in the terminal) through my java application. I have tried using a process Builder as well as a Runtime.exec(). I am able to connect, but the response either says

{"result":0,"reason":"Unknown Json request format"} 

or

{"result":0,"reason":"Invalid client request Json string"}.

Here is the curl command that works in the terminal:

curl -X POST -d '{"command":"get","user":"R16","id":5552}' "http://<Ex.webaddress>"

When running this, the connection fails

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' \"http://<Ex.webaddress\"";try {
        //final Process terminal = curlCommand.start();
        Process p = Runtime.getRuntime().exec(testCurlCommand);
        try {
            String responseString = readInputStream(p.getInputStream());
            JSONObject job = new JSONObject(responseString);

            // job.getString("Parameter")
            statusLabel.setText("Command Result: " + job.toString());
            connectionStatusLabel.setText("Connection Status: Successful");
        } catch (JSONException e) {
            statusLabel.setText("Command Result: 0");
            connectionStatusLabel.setText("Connection Status: Failed");
        }
    } catch (IOException ex) {
        statusLabel.setText("Command Result: 0");
        connectionStatusLabel.setText("Connection Status: Failed");
    }

When changing the String testCurlCommand by removing the quotes around the web address, the connection works but I get the "unknown Json request format"

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' http://<Ex.webaddress";

Ive tried removing the escaped quotes and I still get the same. Ive also tried with process builder:

ProcessBuilder testCurlCommand = new ProcessBuilder("curl", "-X", "POST", "-d", "'{\"command\":\"get\",\"user\":\"R16\",\"id\":\"5552\"}'", "\"http://<examplewebaddress>\"")

I think its something to do with the formating (extra quote or something) but I'm not sure. Any help is greatly appreciated.I will add the readInputStream function below:

static private String readInputStream(InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream, "UTF-8"));
    String tmp;
    StringBuilder sb = new StringBuilder();
    while ((tmp = reader.readLine()) != null) {
        sb.append(tmp).append("\n");
    }
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
        sb.setLength(sb.length() - 1);
    }
    reader.close();
    return sb.toString();
}
2
  • 1
    Why don't you do your POST directly with Java instead? I would be much simpler so easier to maintain and cross-platform Commented Jun 1, 2016 at 15:42
  • Im not sure on how to do that, do you have any links I should visit? Thank you Commented Jun 1, 2016 at 15:46

2 Answers 2

1

For me this looks like a quotation mark issue. Try to be consistent and try to escape the following quotation marks with backslashes

curl -X POST -d '{"command":"get","user":"R16","id":5552}' 'http://<Ex.webaddress>'

PS: I just changed the url quotation marks from " to '

Sign up to request clarification or add additional context in comments.

Comments

1

The best way I know is with DavidWebb.

You could do it with something like:

Response<JSONObject> result = Webb.create().post("http://<Ex.webaddress>")
        .body("{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}").asJsonObject();
JSONObject job = result.getBody();

2 Comments

Thank you! Also what do I need to import? I downloaded the David Webb jar file and added it to the library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.