当前位置:网站首页>CURL命令常用用法
CURL命令常用用法
2022-07-17 00:01:00 【LOOPY_Y】
本文参考文章如下,本文书写内容有限,想了解更多的可以看下面列举的参考文章:
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数据。
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
边栏推荐
- P1321 单词覆盖还原【入门】
- @ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
- Hcia-r & s self use notes (7) (network layer) TTL and loop test, protocol number
- 【MySQL】ERROR 1130 问题解决方案
- 双指针汇总(未完待续)
- SAP ABAP excel export (multiple sheet pages)
- VSCodeUserSetup-x64-1.65.0. exe
- cli和vite通过代理实现跨域
- My creation anniversary
- Uva11362 phone list solution
猜你喜欢

openpyxl 绘制面积图

JVM 内存布局详解,图文并茂,写得太好了!

64 bit interrupt assembly cannot be used

Joint search set

全国职业院校技能大赛网络安全B模块 编码信息获取

Double pointer summary (to be continued)

Press and hold day 27: Service

Hcia-r & s own notes (9) data forwarding process, unicast / multicast / multicast

64位中断汇编不能使用

【MariaDB】启动服务、停止服务、查看状态
随机推荐
为什么加工数据指标
[sort] insert sort, Hill sort and heap sort
[untitled]
【集合】常见操作ArrayList集合的方法
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
Greenplum common
Switch and router technology: Integrated Experiment of hot backup routing protocols HSRP, HSRP and spvstp
NIO三板斧之Buffer,提升程序运行效率的双刃剑
C# 1到100求和 9*9乘法表 成绩等级ABCDE
Rocky基础练习题-find、sed、grep、tar
[deep learning] (II) basic learning notes of deep learning
NVIDIA的jetson利用deepstream加速常见问题分析,deepstream消息发出外部接收,xvaier自启动程序,优秀的博客总结(精二)
@ConditionalOnMissingBean 如何實現覆蓋第三方組件中的 Bean
乐观锁和悲观锁在kubernetes中的应用
Perspective rendering
IDEA开发Servlet项目 如何右键创建servlet
openpyxl 绘制面积图
By voting for the destruction of STI by Dao, seektiger is truly community driven
Acquisition of network security B module coding information of national vocational college skills competition
[daily training] sword finger offer II 041 Average value of sliding window