本文参考文章如下,本文书写内容有限,想了解更多的可以看下面列举的参考文章:
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=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
</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 (#0)
> 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 #0 to host localhost left intact
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数据。文章来源:https://www.toymoban.com/news/detail-448794.html
curl -H "Content-Type: text/xml;charset=UTF-8" -H "SOAPAction: ''" -X POST -d @data.xml http://ip:port/xxxxxService?wsdl
最好补充完整SOAPAction的值:文章来源地址https://www.toymoban.com/news/detail-448794.html
curl -H 'Content-Type: text/xml;charset=UTF-8' -H 'SOAPAction: "testxxx"' -X POST -d @data.xml http://ip:port/xxxxxService?wsdl
到了这里,关于CURL命令常用用法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!