0

I have a curl command:

curl -o map.json http://localhost:8091/pools/default/buckets/travel-sample" 

How I will execute using Java. The command will give me the JSON file, which I have to use to get some attributes from it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    You need HTTP client or read URL as resource. – Suresh Atta Apr 21 '17 at 06:17
  • Why do you have to call curl as opposed to, say, using any of the numerous HTTP APIs that are out there? – Joe C Apr 21 '17 at 06:18
  • As I wanted to execute this command in the server which gives me whole data of what I want. – rekha reddy Apr 21 '17 at 06:27
  • What the comments are suggesting is instead of trying to use `curl` to make the request, use [a HTTP Java library](http://stackoverflow.com/questions/1322335/what-is-the-best-java-library-to-use-for-http-post-get-etc). Most will also parse the json into a java class for you as well which will make the response much easier to use in your java code. – Michael Peyper Apr 21 '17 at 06:57

1 Answers1

1

You can execute your command with :

Runtime.getRuntime().exec("curl -o map.json http://localhost:8091/pools/default/buckets/travel-sample")

It is rather problematic command see this question Downloaded json should be in map.json file after executing command. You can read it using Apache Commons IO util:

FileUtils.readFileToString(new File("map.json"))

Full example is:

Process process;
try {
    process = Runtime.getRuntime().exec("curl -o map.json http://localhost:8091/pools/default/buckets/travel-sample");
    process.waitFor();
    System.out.println(FileUtils.readFileToString(new File("map.json")));
} catch (Exception e) {
    e.printStackTrace();
} 
Community
  • 1
  • 1