curl 简介
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。一般可以用来验证接口是否正常可以调用。
curl 使用
最基本的就是直接curl + 请求地址
curl http://localhost:8080/business/test
这样就是最基本的get请求这个接口,请求响应内容会在控制台输出
-b 携带cookie 调用接口
curl -b 'a=1;b=2' http://localhost:8080/business/test
这样就可以携带a,b两个cookie信息调用接口, a为1 ,b为2.
curl -b cookies.txt http://localhost:8080/business/test
这样可以在进行请求时携带这个文件中的内容为cookie
-d 携带post 参数请求
curl -d 'a=1&b=2' -X POST http://localhost:8080/business/test
-d
是携带post请求的请求参数,使用
-d
时 ,HTTP 请求会自动加上标头
Content-Type : application/x-www-form-urlencoded
。并且会自动将请求转为 POST 方法,因此可以省略
-X POST
, 因此可以简化为
curl -d 'a=1&b=2'http://localhost:8080/business/test
,这样会进行http 的post请求该地址,携带请求参数 a=1和b=2
-H参数添加 HTTP 请求的标头。
curl -H 'Content-Type: application/json' http://localhost:8080/business/test
这样是http get请求地址,携带请求头信息
Content-Type.
,有时候header里是需要携带多个请求头信息的此时就需要添加多个
-H
curl -H 'Content-Type: application/json' -H 'a:1' -d '{"b":"1","c":"2"}' http://localhost:8080/business/test
上面命令添加 HTTP 请求的标头是
Content-Type: application/json
和
a:1
,header携带了这两个请求头信息,然后用-d参数发送 JSON 数据,
-d
会默认是发送
post请求
。
-X参数指定 HTTP 请求的方法。
curl -X GET http://localhost:8080/business/test
这样就是指定
get请求
该地址
本文参考
curl 的用法指南
如需了解更多用法 请看此文章。