接口测试必学之curl命令
curl命令使用
curl命令
curl命令是服务器端调试接口最常用的命令,没有之一
- curl测试GET接口
curl http://www.baidu.com
![curl测试GET接口](https://upload-images.jianshu.io/upload_images/7575721-12c3e1f7fd405fc9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
url参数可以直接写到url中,此时url最好用引号引起来
curl 'http://115.28.108.130:5000/add/?a=1&b=2'
![curl测试带参数get请求](https://upload-images.jianshu.io/upload_images/7575721-e3e60894b6a86883.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 表单类Post请求
curl可以通过-d
指定 请求数据(包含-d
参数时,默认为Post请求),如
curl 'http://115.28.108.130:5000/api/user/login/' \
-d 'name=张三&password=123456'
![curl测试表单类Post请求](https://upload-images.jianshu.io/upload_images/7575721-d654f0a202e9d4b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- JSON格式Post请求
curl可以用个-H
指定header,多个header要使用多个-H
(
curl 'http://115.28.108.130:5000/api/user/reg/' \
-H 'Content-type: application/json' \
-d '{"name":"张三三","password": 123456}'
![curl测试JSON格式Post请求](https://upload-images.jianshu.io/upload_images/7575721-030c00c560f74003.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 带Cookie的请求
Cookie属于header的一项,我们可以携带抓取到的Cookie来请求需要登录的接口,如:
curl 'http://115.28.108.130:5000/api/user/getUserList/' \
-H 'Cookie:PYSESSID=05eac6ba-2eb6-11e9-a130-00163e06e52c;session=eyIwNWVhYzZiYS0yZWI2LTExZTktYTEzMC0wMDE2M2UwNmU1MmMiOnRydWV9.D0Q51A.u9XjrAZb-plUSl01QnNPkGXJPck'
可以使用-v
参数查看请求详情![curl_05.png](https://upload-images.jianshu.io/upload_images/7575721-a931dfcd7226fb5e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
PUT/DELETE等其他请求可以使用-X
指定请求方法
curl -X PUT 'http://.....'
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步