cURL commands in java - java

How to use the cURl command
curl https://na1.salesforce.com/services/data/v20.0/query?q=SELECT+name+from+Account
-H "Authorization: Bearer access_token" -H "X-PrettyPrint:1"
in java to call sales force rest web services

Java has an URLConnection Class that has similar functions as cURL.

For interacting with the salesforce API, it is best to use a client library and not implement it all by yourself, see http://blog.palominolabs.com/2011/03/03/a-new-java-salesforce-api-library/ for an example.

Easiest way is to download cURL java wrapper like https://github.com/pjlegato/curl-java and use cURL directly in your code.
Second way is to use Runtime.getRuntime().exec("crul...") "pattern" and run curl as a normal process.

Related

How to create general Kubernetes objects from yaml using Java client

I want to create a service in Java which will take a yaml file and create whatever object is in there on a Kubernetes cluster.
Somewhat like how kubectl create -f file.yaml does.
The yaml file can contain a pod, service or deployment.
Can this be done in either official Kubernetes Java Client or fabric8 Kubernetes Java Client? How can it be achieved?
You can use Kubernetes REST API
Example create new deployment:
curl -X POST -d #deployment.json -H "Content-Type: application/json" -H "Authorization: Bearer ${ACCESS_TOKEN}" ${API}/apis/extensions/v1beta1/namespaces/${namespace}/deployments

Rest client framework for complex objects

I have developed one web service for order management. This web service takes many complex objects as input parameters. I used curl to test and it works fine. Now I am writing a client but having issue when for ArrayList (e.g. the items are coming as ArrayList) objects. It's sending as String. It's seems the limitation the client framework I am using. I have tried one or two open frameworks but they are not working as expected. It will be great if you can suggest some framework with some examples.
Below is the sample curl request, I have removed some extra parameters to keep it simple.
curl -L -v -b agent_cookies.txt -H "Content-Type: application/json" -d
"{"items":{"atg-rest-class-type":"java.util.ArrayList","atg-rest-values":
[{"atg-rest-class-type":"com.bean.CommerceItemInfo","tinSkuNumber":"41589367","itemNumber":
280594,"color": 9,"size":
94,"salePrice":50.00,"taxAmount":3.5,"stateTax":0.48,"countyTax":0.08,"currencyCode":"USD"},{"atg-rest-class-type":"com..bean.CommerceItemInfo",
"tinSkuNumber":"41589375","itemNumber": 280594,"color": 9,"size":
96,"salePrice":100.00,"taxAmount":7,"stateTax":0.96,"countyTax":0.16,"currencyCode":"USD"}]},orderInfo:{...},"clientAddress":{"atg-rest-class-type":"java.util.ArrayList","atg-rest-values":
[{"atg-rest-class-type":"com.bean.ClientAddress",\"firstName\":\"John\",\"lastName\":\"Dao\",\"state\":\"FL\",\"country\":\"US\",\"postalCode\":\"33606\",\"address1\":\"100
S Edison Avenue\",\"address2\":\"Suite
D\",\"city\":\"Tampa\",\"addressType\":\"BOTH\"}]},{......}}"
http://localhost:8080/rest/model/com/web/actor/CartActor/testOrder
Thank you
After some research I found the limitation of ATG client and no way we can send List.I changed the arguments to accept individual beans only.

Consuming API Curl to Java Application

I have an URL that requires me digest auth using username and password.
curl -k --digest -u user:password https://api.uni5.net/cliente
But have no idea how to use Digest in get Request for Java application.
I'm searching for a simple way, cuz actually we dont use Java in our development, but now we need to use it inside the server.
Thanks for help!

How to give a POST API request in a browser?

I'm really new to APIs and POST or PUT or DELETE. I'm also new to running APIs using POST or other.
I have given a document which says
Function :- Add new Item
URI :- qtp/qtps
ACTION :- POST
REQUEST :- <n1:qtp xmlns:n1="http://www.mac.com/qts/xml/ns/qtm/qtpManagement"><name>rosa qtp 3</name><ipAddress>171.68.121.232</ipAddress><macAddress>10:0t:24:03:r7:57</macAddress><description>this is rosa qtp </description></n1:qtp>
I have absolutely no idea how to proceed further, But I know that by executing the request I need to Add a new Item in the application server, I tried something with browser myself but it did not work.
Can someone show me how can I work with this or explain me more about this or at-least give me a clue
One of the most useful tools for testing and debugging HTTP requests, in my experience, is cURL (http://curl.haxx.se/).
cURL is actually the under-the-hood library used for HTTP requests by a majority of PHP apps; the command-line version lets you do virtually anything that HTTP can do, and get great debugging data.
In the scenario you describe above, after downloading and installing cURL you'd likely use a command like:
curl --header "Content-Type: application/xml" --data '<XML YOU WANT TO SEND>' -X POST <URL TO WHICH DATA SHOULD BE SENT>
It's not clear from your question what the destination host+url is, but using the specific sample data you provide this would probably look like:
curl --header "Content-Type: application/xml" --data '<n1:qtp xmlns:n1="http://www.mac.com/qts/xml/ns/qtm/qtpManagement"><name>rosa qtp 3</name><ipAddress>171.68.121.232</ipAddress><macAddress>10:0t:24:03:r7:57</macAddress><description>this is rosa qtp </description></n1:qtp>' -X POST http://www.mac.com/qtp/qtps
Install a firebug plugin for that. You can use SOA client.

Converting cURL authentication to Java and retrieving & updating data using REST XML (Pt.2)

This is the second part of my question on converting cURL to Java
The first part is titled:
Converting cURL authentication to Java and retrieving & updating data using REST XML (Pt.1)
Third, how can I implement update, create, and delete for the api in Java? For example:
Update: curl -i -X PUT -H "Content-Type:application/xml" -H "Accept: application/xml" -d "<ticket><description>Take this description</description></ticket>" http://user:password#www.assembla.com/spaces/my_space_id/tickets/1
Delete" curl -i -X DELETE -H "Accept: application/xml" http://user:password#www.assembla.com/spaces/my_space_id/tickets/1
Create: curl -i -X POST -H "Content-Type:application/xml" -H "Accept: application/xml" -d "<ticket><summary>This is a Summary</summary><priority>3</priority></ticket>" the weblink
In other words, how can I convert these cURL code into Java?
I would really appreciate your help. Also, a good reference to do such stuff in Java will be awesome too.
Thanks.
Well, the official API usually helps: Assembla REST API
There is says that you need to use basic authentication.
For accessing REST in Java: Rest clients for Java?
you should be looking out for Apache HttpClient tutorials.
There's some Samplecode of a Android Rest Client application around, try searching for it. Also, I just discovered "resting". I haven't tried it, but might be worth a look.
Also watching the Developing Android REST client applications video from Google.io is recommended, as it is teaching some very important architectural basics and hints.
I placed a similar question some time ago, and got an answer here at Stackoverflow.

Categories