Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl command in java

Tags:

First of all , i've already seen couple of documents, stackoverflow questions regarding the same ..I've my project specific question When trying to run command :

   curl -u username:password https://example.com/xyz/abc 

from the mac terminal , I get my desired json format data. But running the same command from java code , I get Unauthorised 401 error in console. My code is :

    String username="myusername";
    String password="mypassword";
    String url="https://www.example.com/xyz/abc";
       String[] command = {"curl", "-u" ,"Accept:application/json", username, ":" , password , url};
        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();
        }

I get Unauthorised 401 error and bunch of html tags . It seems like a repetitive question, but I've tried all the approaches. I know alternative is using http response method, but particularly I want to use curl commands. Thanks in advance.

like image 404
lazy rabbit Avatar asked Jun 25 '15 11:06

lazy rabbit


People also ask

What is cURL command?

curl is a command-line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE). curl is powered by Libcurl. This tool is preferred for automation since it is designed to work without user interaction.

What is cURL example?

curl (short for "Client URL") is a command line tool that enables data transfer over various network protocols. It communicates with a web or application server by specifying a relevant URL and the data that need to be sent or received. curl is powered by libcurl, a portable client-side URL transfer library.

Is cURL Java program?

Curl is a non-java program and must be provided outside your Java program.


2 Answers

Try changing this line

String[] command = {"curl", "-u" ,"Accept:application/json", username, ":" , password , url};

into

String[] command = {"curl", "-H", "Accept:application/json", "-u", username+":"+password , url};
like image 142
Andrzej Bobak Avatar answered Oct 14 '22 16:10

Andrzej Bobak


hey try this I had the same problem. It worked in my terminal had the same error as yours.

String[] command = {"curl", "-u" , username+ ":" + password , url};
like image 40
narmadha Avatar answered Oct 14 '22 16:10

narmadha