本文参考文章如下,本文书写内容有限,想了解更多的可以看下面列举的参考文章: https://www.coonote.com/linux/linux-cmd-curl.html https://www.ruanyifeng.com/blog/2011/09/curl.html https://www.ruanyifeng.com/blog/2019/09/curl-reference.html
1 语法及常用选项
1.1 语法
curl (选项) (参数)
1.2 选项
这里只列举几个我认为比较常用的选项,想要了解更多,可以查看https://www.coonote.com/linux/linux-cmd-curl.html。
选项 | 说明 |
---|
-H / --header | 自定义头信息 | -b / --cookie <name=string/file> | cookie字符串或文件读取位置 | -d / --data | HTTP POST方式传送数据 | -X / --request | 指定什么命令 | -i / --include | 输出时包含回应的标头信息 | -I / --head | 只输出回应的头信息 | -v | 输出通信的整个过程,用于调试 |
?
▉ -H??用于添加HTTP请求头信息
单个请求头信息添加:
curl -H "Content-Type: application/json" http://www.test.com
或者
curl --header "Content-Type: application/json" http://www.test.com
多个请求头信息添加:
curl -H "Content-Type: application/json" —H "Accept-Language: zh-CN" http://www.test.com
等同于
curl -H "Content-Type: application/json; Accept-Language: en-US" http://www.test.com
?
▉ -b??用于向服务器发送Cookie数据
curl -b "name=value" http://www.test.com
或者从cookiesFile中获取到cookie信息
curl -b cookiesFile http://www.test.com
?
▉ -d??用于发送 POST 请求中请求体Body数据
例如:
curl -H 'Content-Type: application/json' -X POST -d '{"name": "july","password": "123456"}' http://www.test.com
将请求体数据放入文件中,配置-d @filename,那么就可以从filename文件中获取到请求体数据,如下:
curl -H 'Content-Type: application/json' -X POST -d @data.txt http://www.test.com
另,需注意,如果请求体数据采用json格式,一定要配置请求头Content-Type为application/json,因为使用-d参数后,HTTP 请求会自动添加请求头Content-Type为application/x-www-form-urlencoded,将请求转为 POST 方法(因此可以省略-X POST)。
?
▉ -X??用于指定 HTTP 的请求方式(GET、POST等)
发送POST请求:
curl -X POST http://www.test.com
发送GET请求:
curl -X GET http://www.test.com
发送DELETE请求:
curl -X DELETE http://www.test.com
?
▉ -i??输出时包含回应的标头信息
命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码或返回信息。
D:\>curl -i https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2443
Content-Type: text/html
Date: Mon, 30 May 2022 12:33:22 GMT
Etag: "588603e2-98b"
Last-Modified: Mon, 23 Jan 2017 13:23:46 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
D:\>
?
▉ -I??只输出回应的头信息
命令收到服务器回应后,只输出服务器回应的标头,不再输出网页的源码或返回信息。
D:\>curl -I https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 30 May 2022 12:33:32 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
D:\>
?
▉ -v??输出通信的整个过程,用于调试
如以下为GET请求调用http://localhost:8082/word?keyWord=water的打印内容:
D:\>curl -v http://localhost:8082/word?keyWord=water
* Trying 127.0.0.1:8082...
* Connected to localhost (127.0.0.1) port 8082 (
> GET /word?keyWord=water HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 30 May 2022 12:15:24 GMT
<
{"code":200,"msg":"操作成功","success":true,"data":[],"time":"2022-05-30 20:15:24"}
* Connection
D:\>
?
2 HTTP接口调用示例
2.1 GET请求
无参数调用:
curl --request GET http://www.test.com
等同于:
curl -X GET http://www.test.com
有参数调用:
curl --request GET http://www.test.com?name=test&password=12345
因curl默认是HTTP GET请求,即也可直接省略-X GET(或 --request GET):
curl https://www.baidu.com
?
2.2 POST请求
curl -H 'Content-Type: application/json' -X POST -d '{"name": "july","password": "123456"}' http://www.test.com
请求体数据从文件data.txt中读取:
curl -H 'Content-Type: application/json' -X POST -d @data.txt http://www.test.com
或省略-X POST:
curl -H 'Content-Type: application/json' -d @data.txt http://www.test.com
?
3 WebService接口调用示例
webservice其实是post请求加上xml数据,所以使用curl post请求模拟webservice。如下,data.xml中为请求的xml数据。
curl -H "Content-Type: text/xml;charset=UTF-8" -H "SOAPAction: ''" -X POST -d @data.xml http://ip:port/xxxxxService?wsdl
最好补充完整SOAPAction的值:
curl -H 'Content-Type: text/xml;charset=UTF-8' -H 'SOAPAction: "testxxx"' -X POST -d @data.xml http://ip:port/xxxxxService?wsdl
|