Elasticsearch REST api through curl not working from java.runtime.exec

691 views Asked by At

I have a java app in which I'm just reading a file and inserting the data into the elasticsearch index.

Here is the code I 'm using.

        in = new BufferedReader(new FileReader("file.txt"));
        line = in.readLine();
        while ((line = in.readLine()) != null) {
            System.out.println("Command: curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'");
            p = Runtime.getRuntime().exec("curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'");
            p.waitFor();

            BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = err.readLine()) != null) {
                 System.out.println(line);
            }
            break;
        }
        in.close();

But this is not working. Curl is giving this error.

curl: (1) Protocol 'http not supported or disabled in libcurl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: "temp"

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: "17.35757"
curl: (3) [globbing] unmatched close brace/bracket in column 1

But see I have printed the command which I'm executing. If I manually copy paste that in console then everything working fine. Why it is happening? What I'm doing wrong?

1

There are 1 answers

2
Daniel Stenberg On

A key is in the error message from curl:

curl: (1) Protocol 'http not supported or disabled in libcurl

The single quote before the protocol name there shows that curl gets that passed as part of the argument, and thus it doesn't work the way you want it to and instead it gives curl a malformed URL.