How to run a curl command from java? [duplicate]

笑着哭i 提交于 2020-08-06 07:10:09

问题


I am trying to make a POST request from Java using curl. It also has some payload. I am getting status code 400. What am I missing?

Following is the curl I use in terminal..

curl --cookie "token=xyzxyzxyzxyz" --header "Content-Type:application/json" --data 
{"branchName":"name","branchId":"bid","sourceBranch":"sb","alias":"pppp","mainPackageFullPath":"main.full.path"}'
-k http://app.aws.application/api/random/create

回答1:


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();
    }


来源:https://stackoverflow.com/questions/53854400/how-to-run-a-curl-command-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!