什么是curl?
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
1.不带任何参数的curl
不带有任何参数时,curl 就是发出 GET 请求。
$ curl https://www.example.com
上面命令向www.example.com发出 GET 请求,服务器返回的内容会在命令行输出。
2.带有参数的curl
-b
-b参数用来向服务器发送 Cookie。
$ curl -b 'foo=bar' ``[https://google.com](https://google.com)
上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
$ curl -b ‘foo1=bar;foo2=bar2’ https://google.com
上面命令发送两个 Cookie。
$ curl -b cookies.txt https://www.google.com
上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
-d
-d参数用于发送 POST 请求的数据体。
$ curl -d'login=emma&password=123'-X POST ``[https://google.com/login](https://google.com/login)
或者
$ curl -d 'login=emma' -d 'password=123' -X POST ``[https://google.com/login](https://google.com/login)
使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。
-d参数可以读取本地文本文件的数据,向服务器发送。
$ curl -d '@data.txt' ``[https://google.com/login](https://google.com/login)
上面命令读取data.txt文件的内容,作为数据体向服务器发送。
-G
-G参数用来构造 URL 的查询字符串。
$ curl -G -d 'q=kitties' -d 'count=20' ``[https://google.com/search](https://google.com/search)
上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略—G,会发出一个 POST 请求。
如果数据需要 URL 编码,可以结合—data—urlencode参数。
$ curl -G —data-urlencode ‘comment=hello world’ https://www.example.com
-H
-H参数添加 HTTP 请求的标头。
$ curl -H 'Accept-Language: en-US' ``[https://google.com](https://google.com)
上面命令添加 HTTP 标头Accept-Language: en-US。
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' ``[https://google.com](https://google.com)
上面命令添加两个 HTTP 标头。
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' ``[https://google.com/login](https://google.com/login)
上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。
-L
-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
-u
-u参数用来设置服务器认证的用户名和密码。
$ curl -u 'bob:12345' ``[https://google.com/login](https://google.com/login)
上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
curl 能够识别 URL 里面的用户名和密码。
$ curl https://bob:12345@google.com/login
上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。
$ curl -u ‘bob’ https://google.com/login
上面命令只设置了用户名,执行后,curl 会提示用户输入密码。
-v
-v参数输出通信的整个过程,用于调试。
$ curl -v ``[https://www.example.com](https://www.example.com)
-x(小写的x)
-x参数指定请求的代理
curl -x 113.185.19.192:80 http://aiezu.com/test.php
-X(大写的X)
-X参数指定 HTTP 请求的方法。
$ curl -X POST ``[https://www.example.com](https://www.example.com)
上面命令对https://www.example.com发出 POST 请求。
实例:
发送带json请求体的post请求
curl -X POST -H "Content-Type:application/json" -d '{"task_id":"handleByData"}' http://user.scoms.svc:8368/cronjob/run/handleByData
curl http://10.85.128.81:30334/employee/tenant/insert -X POST -H "Content-Type:application/json" -d '[{"misId": "heruihao_i","roleType":1,"tenantId":"chengxin"}]'
xml请求
curl -X POST -H "Content-Type:application/xml" -d '<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?><Requestservice="DOWNLOAD_CHECK_ITEM_SERVICE"><Head><AccessCode></AccessCode><Checkword></Checkword></Head><Bodyxsi:type="nwmsCheckSkuSendRequestBodyDTO"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><DownloadCheckItemRequest><Items><Item><TransactionId>164422104951279073483767</TransactionId><SkuTNo>12</SkuTNo><CompanyCode>SCCTEST</CompanyCode><SkuNo>hrh020603</SkuNo><ItemName>SCCTEST</ItemName><WarehouseCode>631DCA</WarehouseCode><Checker></Checker><CheckTime>2022-02-0611:15:49</CheckTime><CheckResult>1</CheckResult><CheckRemarks>审核备注</CheckRemarks></Item><Item><TransactionId>164422104951379073585631</TransactionId><SkuTNo>12</SkuTNo><CompanyCode>SCCTEST</CompanyCode><SkuNo>hrh020604</SkuNo><ItemName>SCCTEST</ItemName><WarehouseCode>631DCA</WarehouseCode><Checker></Checker><CheckTime>2022-02-0612:07:34</CheckTime><CheckResult>1</CheckResult><CheckRemarks>审核备注</CheckRemarks></Item></Items></DownloadCheckItemRequest></Body></Request>' -x 10.206.29.9:1080 http://mtwms-core-web1.sit.sf-express.com/api/interfaces/wms/httpInterface
