0

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
bibek
  • 167
  • 1
  • 4
  • 12
  • You should add what you've tried. Also, I'm not sure what your use case is, but you might be better off making a post request directly through java, as opposed to running a curl command. – robert Dec 19 '18 at 16:33

1 Answers1

5

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();
    }
Vinay Hegde
  • 1,424
  • 1
  • 10
  • 23
  • You must specify the full path to the "curl" command - check it with "which curl"! Unless you'll get the error "Cannot run program curl, createProcess error = 2" – Shai Alon Feb 22 '22 at 07:48
  • Shouldn't the Process object be destroyed here? – mang4521 Nov 10 '22 at 10:44