/* Other URL * ciURL = "http://localhost:8080/api/cmdb/ci/"; * ciTypeURL = "http://localhost:8080/api/cmdb/citype/"; * ciRelationshipsURL = "http://localhost:8080/api/cmdb/cirelationships/" * * * OPERATION_NAME : [ "add", "update" , "read" , "delete" ] * format : ["json","xml"] If format not specified, json is the default. * * Dependency jars : httpcore-4.0.1.jar , httpclient-4.0.1.jar : This can be available http://hc.apache.org/downloads.cgi * @author: nprasanna */
package api; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair;
public class PostAPIRequest { public static void main(String[] args) throws Exception { HttpClient client = new DefaultHttpClient(); String postURL = "http://localhost:8080/api/cmdb/citypeattributes/"; HttpPost post = new HttpPost(postURL); String fileName = "D:\\Downloads\\API-INPUT-XMLl\\ADD_ATTRIBUTE\\pick-list.xml"; BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF8")); String str; StringBuilder strContent = new StringBuilder(); while ((str = in.readLine()) != null) { strContent.append(str); } String fileContents = strContent.toString(); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("TECHNICIAN_KEY", "31F022AE-4AAC-4F53-A519-193727C8B2BB")); nameValuePairs.add(new BasicNameValuePair("format", "xml")); nameValuePairs.add(new BasicNameValuePair("OPERATION_NAME", "add")); nameValuePairs.add(new BasicNameValuePair("INPUT_DATA", fileContents)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |