简介:
进入API操作的第一步就是登陆,网上资源很少,在些做个总结,转载请注明出处
1.WEB上验证 api
地址:http://jsfiddle.net/U6qwK/6/
输入你的URL、Login_name、Password ,查看调用结果(支持本地验证哦!)
2.通过Curl 命令行验证
curl -X POST -H 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8' -d 'auth_user=itop&auth_pwd=213xxx&json_data=%7B%22operation%22%3A%22list_operations%22%7D' "http://IP/web/webservices/rest.php?version=1.3"
或者
curl -X POST -H 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8' -d 'json_data={"operation": "list_operations"}' -d 'auth_user=itop&auth_pwd=1cc0zx' http://100.713.191.60/web/webservices/rest.php?version=1.3
3. 写python 脚本验证
#!/usr/local/bin/python #coding:utf-8 import urllib import urllib2 import json def http_post(): url="http://100.173.191.60/web/webservices/rest.php?version=1.3" auth ={"auth_user": "itop" , "auth_pwd": "2134xxzx"} opr = {"operation": "list_operations"} oprjson =urllib.urlencode({'json_data': json.dumps(opr)} ) jdata = urllib.urlencode(auth) jdata = jdata+'&'+oprjson response = urllib.urlopen(url,jdata) return response.read() rep=http_post() print rep
执行结果:
问题:
1.必须分开用户与json_data, 比如下面就不会成功
curl -X POST -H 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8' -d '{"auth_user":"itop","auth_pwd":"211cc0zx", "json_data":{"operation":"list_operations"}}' http://100.173.191.60/web/webservices/rest.php?version=1.3
2. curl 的-H 选项可以不要
也可以换成 -H "Content-Type: application/json" 。 浏览器默认会变成最 -H 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'
参考资料: