您的位置:首页 > 运维架构 > Linux

Linux安装、使用curl进行Get、Post接口调试

2017-10-30 14:00 573 查看
一、linux中如何安装Curl,步骤如下:1.下载wget http://curl.haxx.se/download/curl-7.38.0.tar.gz注意:如果命令执行失败,请使用浏览器打开链接进行下载,然后在Linux上使用“rz”命令上传用浏览器下载好的文件,再执行下面的步骤。
2.解压tar -xzvf curl-7.38.0.tar.gz3.安装cd curl-7.38.0./configuremakemake install4.完成

二、linux中使用Curl发起Get、Post请求:Windows/mac环境上有很多接口测试的工具,如soapui、postman等,但这些工具在linux平台上使用起来相对不那么便捷。 有时候当我们要测试一些外部接口时,当本地无权调用测试路径时,需要将测试建立在linux平台,除了封装简单的请求代码进行实现外,可通过curl工具实现

1、测试get请求

$ curl http://www.linuxidc.com/login.cgi?user=test001&password=123456

2、测试post请求

$ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi

3、xml格式post请求

接口如下:


请求方式:

方式一:发送磁盘上面的xml文件(推荐)

[plain] view plain copyroot [ /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文件,后面为请求路径






方式二:在命令行直接发送xml结构数据

[plain] view plain copyroot [ /apps ]$ curl -H 'content-type: application/xml' -X POST -d '<?xml version="1.0" encoding="UTF-8"?><userinfoReq><subsNumber>13814528620</subsNumber><type>3</type></userinfoReq>' http://172.19.219.xx:8081/csp/faq/actDiaUserInfo.action 或者
[plain] view plain copyroot [ /apps ]$ echo '<?xml version="1.0" encoding="UTF-8"?><userinfoReq><subsNumber>13814528620</subsNumber><type>3</type></userinfoReq>'|curl -X POST -H'Content-type:text/xm' -d @- http://172.19.xx.xx:8081/csp/faq/actDiaUserInfo.action
ps:其中<?xml version...>就是要post的xml 文件,后面是请求路径,linux上双引号或单引号之间嵌套需要使用反斜杠 \ 进行转义
响应消息:[plain] view plain copy<?xml version="1.0" encoding="utf-8"?><result><result_code>0</result_code><result_text>success</result_text><getUserinfoRsp><userInfo><identityID>1117384802</identityID><phone>13814528620</phone><email>13814528620@139.com</email><accountID></accountID><province>江苏</province><city>南京</city><passId>7775637869243</passId></userInfo></getUserinfoRsp></result>

4、json格式post请求

接口如下:


请求消息:

方式一:发送磁盘上面的JSON文件(推荐)

[plain] view plain copyroot [ /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文件,后面为请求路径


方式二:在命令行直接发送JSON结构数据

[plain] view plain copyroot [ ~ ]$ 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 响应消息:
[plain] view plain copy{"code":"4000","message":"参数错误:time的值不是UInt"}

参考文章:http://blog.csdn.net/russ44/article/details/53308838 http://blog.csdn.net/makenothing/article/details/39250491
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux http post