How to Use Curl in Java

How to use cURL in Java?

You can make use of java.net.URL and/or java.net.URLConnection.

URL url = new URL("https://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}

Also see the Oracle's simple tutorial on the subject. It's however a bit verbose. To end up with less verbose code, you may want to consider Apache HttpClient instead.

By the way: if your next question is "How to process HTML result?", then the answer is "Use a HTML parser. No, don't use regex for this.".

See also:

  • How to use java.net.URLConnection to fire and handle HTTP requests?
  • What are the pros and cons of the leading Java HTML parsers?

Java executing curl command not working on some commands

I made a minimal example to explain you a few things:

public class Playground {
public static void main(String... args) throws IOException, InterruptedException {
String cmdGetDocId = "curl -XGET 'https://google.com'";
Process process = Runtime.getRuntime().exec(cmdGetDocId);
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println("output: ");
Thread.sleep(2000);
while(process.isAlive()) Thread.sleep(100);
System.out.println("return value: " + process.exitValue());
reader.lines().forEach(System.out::println);
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
reader.lines().forEach(System.err::println);
System.out.println("---");
}
}

On the one hand, you must make sure, the command is actually finished, when you try to output. In order to make sure, I have this dirty while-loop. Also, you want to have a look at the Error-Output, too.

Also you actually don't want to use cURL inside of your Java Program. There is a ton of beautiful Libraries or even the bare HttpURLConnection.

How to run a curl command from java?

You can do it like this

    String branchName="name";
String branchId="bid";
String sourceBranch="sb"
String alias="ppp"

String[] command = {"curl" "-k" "-i" "-X" POST "-H" "Content-Type: multipart/form-data" --cookie "rsession=your rsession" "Content-Type:application/json" --data{branchName+":"+branchId":"+sourceBranch":",+alias}};
ProcessBuilder process = new ProcessBuilder(command);
Process p;
try
{
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print(result);

}
catch (IOException e)
{ System.out.print("error");
e.printStackTrace();
}

Execute Curl from Java

In 'command', try removing the single quotes from

"'admin2:0xdRv63RKq2MtA326BNGQAI6yA1QNGO09enamGxI'"

so that it becomes

"admin2:0xdRv63RKq2MtA326BNGQAI6yA1QNGO09enamGxI"

and see if that helps. It might be considering them as part of the actual username and password.

I think the issue is the Json in this case and I think you are right then that the double quotes are the issue. Try putting the Json in a file and use curl to send the file contents as the body of your message.

String[] command = {"curl", "-k", "-v", "-u","admin2:0xdRv63RKq2MtA326BNGQAI6yA1QNGO09enamGxI",
"-d", "@/path/to/filename.json", "-H", "Content-Type: application/json", "https://192.168.101.59/api/v1/auth/"};

Java executing curl command using HttpURLConnection returns 204 (HTTP_NO_CONTENT)

Did you try, setting the same user agent in your Java Code, cURL would use? something like curl/7.37.0?
As far as I can tell, that should be all, what differs. Aside cURL following redirects. But as there is no redirect, I guess it might be the User Agent making a difference.

There are a lot of server applications, behaving differently, when they are called by a browser (Like you make it think by setting the User-Agent to Mozilla/5.0), instead of some other application, like cURL.



Related Topics



Leave a reply



Submit