对于接口文档,有时会使用Linux下的curl的方式给出,很多接口测试工具也支持将测试用例导出为curl格式。对于后端测试,还是需要掌握curl,进行接口快速验证。

2eeee88317955aaa31163c1690741474.png
# http://jwd.funnyapi.com/#/index

经纬度行政区域查询接口API
http://116.196.105.215:1234/gis

使用curl测试
curl 'http://116.196.105.215:1234/gis?auth_user=freevip&latitude=39.880655&longitude=116.354386'

curl是非常方便的Rest客户端,可以很方便的完成Rest API测试,利用curl对http协议发送GET、POST、DELETE、PUT,同时还支持携带header来满足Rest API需求的特定条件。

一、curl常用的参数

  • -X/--request,指定http method
  • -H/--header,设定request里的header
  • -i/--include,显示response的header
  • -d/--data,设定http parameters
  • -v/--verbose,输出详细信息
  • -u/--user,使用者账号
  • -b/--cookie,cookie文件路径

二、实战

A、设置header

# 设置header
curl -i -H "Content-Type: text/plain; charset=UTF-8" http://10.255.242.157:8082/preorder/query?orderId=1013035382550

cf19b1a975aa4b43208bcdc59e7d4acc.png

B、设置parameter

# 设置parameter
curl -X POST -d "param1:value1&param2=value2" 

或者
curl -X POST -d "param1=value1" -d "param2=value2"

curl -i -X POST -d "partnerId=29&remark=价格&userMail=isisiwish#qq.com&userName=isisiwish" http://10.255.242.128:8888/alarm/email/add

c32858ccf24479e1ac65db6b0c7fbe8d.png

99aa65fe5dd91ee11eed32782e5a6b5a.png

C、使用cookie

# 使用cookie文件
curl -i --header "Content-Type:application/json" -X GET -b cookie.txt http://www.baidu.com

# 使用header传递cookie
curl 'https://www.zhihu.com/api/v4/me?include=email' -H 'cookie: _xsrf=xxoo'

474f6bfa896988782221479be1de934d.png

D、文件上传

# 文件上传
curl -i -X POST -F 'file=@/User/uploadFile.txt' -H "token:abc123" -v

E、HTTP Basic Authentication

# HTTP基本认证(HTTP Basic Authentication)
curl -i -u root:root 'http://10.255.242.168:12306/governance/services'

b1e2b20815db049a0f09911e9e779001.png

9788d241d9d1aa9e1509b8d7d95b7f46.png

F、xml或json作为参数

# json作为直接入参
curl -i -H 'Content-Type:application/json' -X POST -d '{"partnerId": "28","addressCn": "安徽省|亳州市|利辛县"}' 'http://10.255.242.168:8500/address/ddAddressGet'

c0243a4420d9562e60393661862c73bd.png
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐