ctoan
asked on
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/j son \
-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":"primaryIdentityDo main","val ue":"p0-id entity-dom ain"},
{"name" : "defaultTarget" , "value" : "VirtualTarget-0"},
{"name" : "securityRealm" , "value" : "myrealm"}
]}" \
-X POST http://myhost.us.company.com:8971/management/lifecycle/latest/runtimes/MyTestRuntime/partitions
curl -v \
--user admin:admin123 \
-H X-Requested-By:MyClient \
-H Accept:application/json \
-H Content-Type:application/j
-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":"primaryIdentityDo
{"name" : "defaultTarget" , "value" : "VirtualTarget-0"},
{"name" : "securityRealm" , "value" : "myrealm"}
]}" \
-X POST http://myhost.us.company.com:8971/management/lifecycle/latest/runtimes/MyTestRuntime/partitions
You can use libcurl java bindings.
ASKER
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?
ASKER
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.
Thanks.
Thanks.
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....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
ASKER
Didn't really receive any useful information from anyone else. All the info provided was already stated in my question.