熟悉使用curl调试api
大约 1 分钟
前言
cURL
,一个很强大的http api调试命令行工具,可以在Shell上直接使用 有必要掌握基础使用方法。
安装
https://curl.haxx.se/download.html
常见使用
- 发起GET 请求
curl -i http://localhost:8080/api
- 携带cookies发起请求
curl -b 'a=1;b=2' http://localhost:8080/api
- 保存cookies到文件并使用
curl -c /tmp/mycookies http://localhost:8080/api
curl -b @/tmp/mycookies http://localhost:8080/api
- 携带header发起请求,可以多个-H参数
curl -H 'Content-Type:application/json'
-H 'CustomHeader=hello' http://localhost:8080/api
- 发起POST(携带json)
curl -XPOST -H 'Content-Type:application/json' -d '{"id:1","name:lin"} http://localhost:8080/api
- 发起POST(携带json文件)
- 发起POST(携带json)
```shell
curl -X POST -H "Content-Type: application/json" -d @/tmp/cats.json http://localhost:8080/api
- 发起POST (携带KV, application/x-www-form-urlencoded)
```shell
curl -d 'k1=value1&k2=value2' http://localhost:8080/api
- 携带User-Agent
curl -A 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0' http://localhost:8080/api
扩展
- 可以使用POSTMAN帮忙生成curl参数,如图: