AssetExplorer

    How to access API using Third-Party Tools?

     

     

     

     /*  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();
            }
        }
    }



    Python using HTTP for request

     

     

    #requests is a python module used for Posting HTTP requests.
    #module can be downloaded from https://pypi.python.org/pypi/requests
    #Getting the input xml file as argument and post the xml contents in the INPUT_DATA attribute
    import requests,codecs,sys,pprint
    url = 'http://localhost:8080/api/cmdb/ci/'
    fileName = sys.argv[1]
    xmlcontent = open(fileName, 'r').read()
    args = {'OPERATION_NAME':'add','TECHNICIAN_KEY':'398DA976-0242-47AF-BE43-F6377AEC3EBA','format':'xml','INPUT_DATA':xmlcontent}
    response = requests.post(url,params=args)
    print response.text

     



    Browser Plugins:

    • Firefox : https://addons.mozilla.org/en-us/firefox/addon/restclient/
    • Chrome:  PostMan :  http://bit.ly/MuID5O

     

     

     

     

     

    Copyright © 2017, ZOHO Corp. All Rights Reserved.