Windows/mac环境上有很多接口测试的工具,如soapui、postman等,但这些工具在linux平台上使用起来相对不那么便捷。
有时候当我们要测试一些外部接口时,当本地无权调用测试路径时,需要将测试建立在linux平台,除了封装简单的请求代码进行实现外,可通过curl工具实现
$ curl http://www.linuxidc.com/login.cgi?user=test001&password=123456
$ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi
root [ /apps ]$ curl -X POST -H 'content-type: application/xml' -d @/apps/myxmlfile.txt http://172.19.219.xx:8081/csp/faq/actDiaUserInfo.action
ps:其中myxmlfile.txt为磁盘上面的xml文件,后面为请求路径
root [ /apps ]$ curl -H 'content-type: application/xml' -X POST -d '13814528620 3 ' http://172.19.219.xx:8081/csp/faq/actDiaUserInfo.action
或者
root [ /apps ]$ echo '13814528620 3 '|curl -X POST -H'Content-type:text/xm' -d @- http://172.19.xx.xx:8081/csp/faq/actDiaUserInfo.action
ps:其中就是要post的xml 文件,后面是请求路径,linux上双引号或单引号之间嵌套需要使用反斜杠 \ 进行转义
响应消息:
0 success 1117384802 13814528620 [email protected] 江苏 南京 7775637869243
root [ /apps ]$ curl -X POST -H 'content-type: application/json' -d @/apps/myjsonfile.txt http://192.168.129.xx/AntiRushServer/api/ActivityAntiRush
ps:其中myjsonfile.txt为磁盘上面的JSON文件,后面为请求路径
root [ ~ ]$ curl -H 'content-type: application/json' -X POST -d '{"accountType":"4","channel":"1","channelId":"YW_MMY","uid":"13154897541","phoneNumber":"13154897541","loginSource":"3","loginType":"1","userIp":"192.168.2.3","postTime":"14633fffffffffff81286","userAgent":"Windows NT","imei":"352600051025733","macAddress":"40:92:d4:cb:46:43","serialNumber":"123"}' http://192.168.129.xx/AntiRushServer/api/ActivityAntiRush
响应消息:
{"code":"4000","message":"参数错误:time的值不是UInt"}
root [ /apps ]$ curl -H 'Content-Type: text/xml;charset=UTF-8;SOAPAction:""' -d @/apps/mysoapfile.xml http://172.18.173.xx:8085/csp-magent-client/madapterservices/madapter/lmCountAccessor
ps:其中myjsonfile.txt为磁盘上面的请求报文文件,后面为请求路径
root [ /apps ]$ curl -H 'content-type: application/xml' -d '1600 10 6496388382726 13814528620 ]]> ' http://172.18.173.xx:8085/csp-magent-client/madapterservices/madapter/lmCountAccessor
响应消息
8