Search code examples
javahttpcurlhttp-postinfluxdb

use Java client like curl with param


I use influxdb 0.9. in this version, i can write database like

curl -XPOST 'http://localhost:8086/write?db=mydb' -d 'cpu,host=server01,region=uswest value=1.0'

Now I convert it to java

URL url = new URL("http", "localhost", 8086, "/write?db=mydb");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream wr = con.getOutputStream();
Stirng s = "cpu,host=server01,region=uswest value=51.0";
wr.write(s.getBytes(UTF_8));
wr.flush();
wr.close();

but it doesn't work. Is the "-d" meant to represent post parameters? How can I express that in Java?


Solution

  • In this example the curl flag should really be --data-binary, not -d, which can have different encoding. As long as your string is unaltered by the Java code it should be fine. Anything like URL encoding will prevent the line protocol insert from working.