使用例子
curl --socks5 192.168.0.104:10808 https://api.ipify.org/?format=json
curl --proxy http://192.168.0.104:10809 https://api.ipify.org/?format=json
curl -m 30 --retry 3 --socks5 127.0.0.1:10808 https://api.ipify.org/?format=json
curl -m 30 --retry 3 --socks5 192.168.163.1:10808 https://api.ipify.org/?format=json
curl telnet://34.22.88.102:8888 -v
# 提交 json
# see https://www.endpoints.dev/
curl -X POST --url https://xxxxx.endpoints.dev --header 'Content-Type: application/json' \
--data '{"site":"blog.196000.xyz","date":"2025-07-27"}'
# 上传文件,enctype='multipart/form-data'
curl --form upload=@localfilename --form press=OK [URL]
参数详解
-
-X
/--request
参数,指定什么命令,如 GET、POST、DELETE。 -
-d
/--data
参数,HTTP POST方式传送数据。 -
-H
/--header
参数,自定义头信息传递给服务器。 -
-c
/--cookie-jar
参数,操作结束后把 cookie 写入到这个文件中。 -
-b
/--cookie
参数,cookie 字符串或文件读取位置。 -
-F
/--form /--form-string
参数,模拟 http 表单提交数据。 -
-O
/--remote-name
,把输出写到该文件中,保留远程文件的文件名。 -
-o
/--output
, 把输出写到指定文件中。 -
-s
/--silent
命令,静默模式,不输出任何东西。 -
-i
/--include
,输出时包括 response 头信息。 -
-I
/--head
,只显示请求头信息。 -
-A
参数,指定客户端的用户代理标头,即User-Agent。 -
--data-urlencode
参数,等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。 -
-e
参数,用来设置 HTTP 的标头Referer,表示请求的来源。 -
-G
参数,用来构造 URL 的查询字符串。 -
-k
参数,指定跳过 SSL 检测。 -
-L
参数,会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。 -
--limit-rate
用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。 将带宽限制在每秒 200K 字节curl --limit-rate 200k https://google.com
-
-S
参数,指定只输出错误信息,通常与-s一起使用。 -
-u
参数,用来设置服务器认证的用户名和密码。设置用户名为name,密码为password,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1
。curl --user name:password example.com
-
-v
参数,输出通信的整个过程,用于调试。 -
--trace
参数,也可以用于调试,还会输出原始的二进制数据。 -
-x
参数,指定 HTTP 请求的代理。如果没有指定代理协议,默认为 HTTP。curl -x socks5://james:[email protected]:8080 https://www.example.com
参数使用
-X
/--request <command>
参数
指定什么命令,如 GET、POST。
例:
curl -X GET http://localhost:8080/search?data=123 # -X GET是可选的
curl -X POST -d "data=123&key=456" http://localhost:8080/search``
# JSON数据以 请求体(body) 方请求
curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/
-d
/--data <data>
参数
HTTP POST方式传送数据。
-d 选项是以使用 POST 方式向 Server 发送数据,因此在使用 -d 的时候,可以省略 -X POST。请注意,使用 -d 时,将使用 Content-type:application/x-www-form-urlencoded
方式发送数据。如果想使用 JSON 形式 post 数据,可以使用 -H 指定头部类型。如:
curl -H "Content-Type:application/json" -d '{"data":"123","key":"456"}' http://localhost:8080/search
-H
/--header <line>
参数
自定义头信息传递给服务器。
例:
curl -H "Host:192.168.0.1" -H "accept-language:zh-cn" URL
-c
/--cookie-jar <file>
参数
操作结束后把 cookie 写入到这个文件中。
例:
curl -d "name=zhangsan&password=123" http://localhost:8080/login -c ./cookie
-b/--cookie <name=string/file>
参数
cookie 字符串或文件读取位置。
例:
# cookie 文件
curl http://localhost:8080/login -b ./cookie
# 直接指定 cookie
curl --cookie "name=zhangsan" http://localhost:8080/login
-F/--form <name=content>
/--form-string <name=string>
参数
模拟 http 表单提交数据,curl 可以通过 -F
命令来以 Content-Type:multipart/form-data
的形式向 server post 数据,该命令允许提交二进制文件等。可以使用 @
前缀来制定提交的内容为一个文件,也可以使用 <
符号来提交文件中的内容。
例:
# 向服务器上传一个图片,图片的表单 name 为 profile,内容为 protrait.jpg 的二进制
curl -F [email protected] https://example.com/upload
-O
/--remote-name
把输出写到该文件中,保留远程文件的文件名。
例:
curl http://man.linuxde.net/text.iso --silent -O
-o
/--output
把输出写到指定文件中。
例:
# 选项 -o 将下载数据写入到指定名称的文件中,并使用 --progress 显示进度条:
curl http://man.linuxde.net/test.iso -o filename.iso --progress
######################################### 100.0%
-s
/--silent
命令
静默模式,不输出任何东西。
curl http://man.linuxde.net/text.iso --silent -O
-i
/--include
输出时包括 response 头信息。
例:
curl -i https://www.baidu.com
-I/--head
只显示请求头信息。
例:
curl -I https://www.baidu.com
curl 有两个超时时间:一个是连接超时时间,另一个是整个过程允许的最大时间
-
--connect-timeout <seconds>
Maximum time in seconds that you allow the connection to the server to take. This only limits the connection phase, once curl has connected this option is of no more use. See also the -m/–max-time option. If this option is used several times, the last one will be used.
这个是指定连接超时时间。 如果出错, 提示形如:curl: (28) connect() timed out!
-
-m/--max-time <seconds>
Maximum time in seconds that you allow the whole operation to take. This is useful for preventing your batch jobs from hanging for hours due to slow networks or links going down. See also the –connect-timeout option. If this option is used several times, the last one will be used.
这个是指定最大的允许时间。 出错提示如:curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
还可以这样用: curl -o x.log "http://www.abc.com" --speed-time 5 --speed-limit 1
将url内容保存到x.log中, 如果传输速度小于1字节/秒的状态持续5秒,该连接就会终止.
连接超时时间用 –connect-timeout 参数来指定,数据传输的最大允许时间用 -m 参数来指定。
例如:curl --connect-timeout 10 -m 20 "http://www.abc.com"
连接超时的话,出错提示形如:curl: (28) connect() timed out!
数据传输的最大允许时间超时的话,出错提示形如:curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received
curl 输出连接时间
[root@localhost ~]# curl -L --output /dev/null --silent --show-error \
--write-out 'time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_redirect: %{time_redirect}\ntime_starttransfer: %{time_starttransfer}\n----------\ntime_total: %{time_total}\n' \
'https://www.baidu.com'
time_namelookup: 0.253
time_connect: 0.292
time_appconnect: 0.498
time_pretransfer: 0.498
time_redirect: 0.000
time_starttransfer: 0.549
----------
time_total: 0.549
监控指标的制定
在制定监控指标时可以参考以下内容作为指导:
Google SRE 黄金指标(面向用户系统中最重要的衡量因素)
- 延迟(Latency): 延迟是发送请求和接收响应所需的时间。
- 通讯量(Utilization): 监控当前系统的流量,用户衡量服务的容量需求。
- 饱和度(Saturation):衡量当前服务的饱和度。
- 错误(Errors):监控当前系统所有发生的错误请求,衡量当前系统错误发生的速率。
Weave Cloud RED方法(微服务架构应用的监控和度量)
- (请求)速率(Rate):服务每秒接收的请求数。
- (请求)错误(Errors ):每秒失败的请求数。
- (请求)耗时(Duration):每个请求的耗时。
USE方法(系统性能)
- 使用率(Utilization):关注系统资源的使用情况。 这里的资源主要包括但不限于:CPU,内存,网络,磁盘等等。100%的使用率通常是系统性能瓶颈的标志。
- 饱和度(Saturation):例如CPU的平均运行排队长度,这里主要是针对资源的饱和度(注意,不同于4大黄金信号)。任何资源在某种程度上的饱和都可能导致系统性能的下降。
- 错误(Errors):错误计数。