如何使用第三方工具访问 API?

 

 

 /*  其他 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"] 如果未指定 format,默认值为 json。

         *  

         * 依赖 jar 文件: httpcore-4.0.1.jar , httpclient-4.0.1.jar :可从 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();

        }

    }

}
 

 


使用 HTTP 请求的 Python 
 

 

 

#requests 是一个用于发送 HTTP 请求的 Python 模块。

#该模块可从 https://pypi.python.org/pypi/requests 下载

#将输入的 xml 文件作为参数获取,并将 xml 内容发布到 INPUT_DATA 属性中

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

 

浏览器 插件:

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