1

I'm trying to send a JSON string using CURL command and Runtime.getRuntime.exec() function.

My JSON string is something like:

String jsonString = "{\"object\":[\"something\",\"another something\"]}"

I'm trying to send this string using the following function:

Process p;
p = Runtime.getRuntime().exec(new String[] {"curl","someURL","-H","Content-Type:application/json","-d",jsonString,"-u","something:something"}

Once I execute the following lines and parse the output, I get an error saying that the JSON document is not valid. When I try the same command using command line, it works just fine. I think the problem is with the JSON string as the escape characters are also being send as a part of the JOSN data and hence the invalid JSON data output.

Is there anything that I have done wrong or is there any other way that I have to execute the command.

1
  • I faced the problem. i made a string and passed the string in Runtime.getRuntime().exec("command_string") But this didnt work. my string command was - "curl --header \"Content-Type: application/json\" --data '{\"username\":\"xyz\",\"password\":\"xyz\"}' --request POST localhost:8080/mm/connectiondebugger/jerseydebuggerapi" when i tried the code in your question - it worked for me :) Thanks. Commented Aug 2, 2018 at 12:36

1 Answer 1

2

Just tried like with a modification and worked

    String jsonString = "{\"object\":[\"something\",\"another something\"]}";

    ProcessBuilder ps = new ProcessBuilder(new String[] { "curl", "http://localhost:8338", "-H",
            "Content-Type:application/json", "-d", jsonString, "-u", "something:something" });
    ps.redirectErrorStream(true);
    Process pr = ps.start();  

    BufferedReader in = new BufferedReader(new 

    InputStreamReader(pr.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    pr.waitFor();

    in.close();
    System.exit(0);

For server side I used Pippo webframework and it returned me an OK string

     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed

    0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:-     -:--     0
    100    46    0     2  100    44    329   7247 --:--:-- --:--:-- --:--:--  7333
    OK

Server side code:

public class PippoApplication extends Application {

private final static Logger log = LoggerFactory.getLogger(PippoApplication.class);

@Override
protected void onInit() {


    POST("/", new RouteHandler() {

        @Override
        public void handle(RouteContext routeContext) {
            System.out.println(routeContext.getRequest().getBody());
            routeContext.send("OK");
        }
    });


}

}

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

Comments

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.