• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1399
  • Last Modified:

java equivalent for curl post command

I am looking for an equivalent in java for the following  curl command. I know I can do a Runtime.exec() etc to run this, but I was looking for a way to do this using some Jersey client API or some other similar solutions.

curl -v \
--user admin:admin123 \
-H X-Requested-By:MyClient \
-H Accept:application/json \
-H Content-Type:application/json \
-d "{"name":"Partition0",
"properties" : [
{ "name" : "resourceGroups",
        "properties" : [
                { "name" : "g1",
                "properties" : [
                        { "name" : "resourceGroupTemplate", "value" : "template1" },
                        { "name" : "targets" , "value" : "VirtualTarget-0"}]}]},
{"name" : "availableTargets" , "value" : "VirtualTarget-0"},
{"name" : "defaultTarget" , "value" : "VirtualTarget-0"},
{"name" : "securityRealm" , "value" : "myrealm"}
]}" \
-X POST http://myhost.us.company.com:8971/management/lifecycle/latest/runtimes/MyTestRuntime/partitions
  • 4
  • 4
1 Solution
You can use libcurl java bindings.
ctoanAuthor Commented:
I did consider libcurl, but I heard the code may not be portable and may not work on all platforms. Moreover, I don't see any good examples of libcurl with Java.
Why you do all that in CURL firsthand? Java has it's web services and restful services APIs builtin?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ctoanAuthor Commented:
Well, I'm new to this and that's why I want to know how this can be done in Java. I got this curl command and would like to know the Java equivalent. I have seen examples using Jersey Client API and HttpClient etc, but I'm not sure how this particular example I posted can be converted.

it is done using official APIs, like jersay client api you mentioned, completely abstracting from having linux or windows or solaris, using http/1.0 or  http/2.0 over ipv4 or ipv6, using SSL or not....
ctoanAuthor Commented:
The example at the following link (with some changes) helped me to get this working


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import javax.ws.rs.core.MediaType;

public class JerseyClientPost {

      public static void main(String[] args) {

            try {

                  Client client = Client.create();
                  client.addFilter(new HTTPBasicAuthFilter("admin","admin123"));

                  WebResource webResource = client
                  String input = ""{"name\":"Partition0\",\"properties\" : [{ \"name\" : \"resourceGroups\",\"properties\" : [{ \"name\" : \"g1\", ....... ";

                  JSONObject json = new JSONObject(input);

                  ClientResponse response = webResource.type("application/json")
                              .header("X-Requested-By", "MyClient")
                              .header("Accept", "application/json")
                              .post(ClientResponse.class, json.toString());

                  if (response.getStatus() != 201) {
                        throw new RuntimeException("Failed : HTTP error code : "
                                    + response.getStatus()+ response.toString());

                  System.out.println("Output from Server .... \n");
                  String output = response.getEntity(String.class);

            } catch (Exception e) {




You should use JSR 353 API and not call jetson classes directly. Sooner or later (read java 9 or Java 10) such sidestep will be punished with deprecation. At that moment you will have too much codebase to change it to normal API and your app will be stuck with old java version (like java 5 is burried end of may, and java7 gets costly in september)
ctoanAuthor Commented:
Didn't really receive any useful information from anyone else. All the info provided was already stated in my question.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now