Skip to content

cURL

cURL(Client for URL 简写,英文读音 /kɝl/,类似拼音 ker)是瑞典程序员 Daniel Stenberg 用 C 语言实现支持多种网络协议传输数据软件包,它提供可二次编程库和命令行工具使用。

首个版本发布于 1996 年,由于功能完善、跨平台、免费开源、支持互联网协议族应用层大部分常见常用协议(包括但不限于 HTTP IMAP RTMP WS SMB MQTT IPv6),成为不成文约定标准客户端。

基本用法

下面如无特别说明,以使用命令行工具 curl 为例说明常见用法。

查看版本和支持协议、特性 curl --version;显示发出请求和收到响应细节

sh
# 显示请求细节
curl -v example.com

# 只显示响应头
curl -sD - -o /dev/null example.com

# 显示请求和响应跟踪信息
curl --trace trace.txt example.com
curl --trace-ascii trace.txt example.com

结合 httpbin.org 网站可以将发出请求以响应方式回显

curl https://httpbin.org/get

json
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.86.0", 
    "X-Amzn-Trace-Id": "Root=1-1234"
  }, 
  "origin": "123.123.123.123", 
  "url": "https://httpbin.org/get"
}

指定 HTTP 协议版本和端口(不指定则默认用 HTTP 1.1) curl --http2 https://example.com:888

后面例子为了方便,省去域名的 https:// 前缀。

指定 HTTP 请求方法 curl -XPUT http://example.com

指定 HTTP 请求协议版本和端口 curl https://example.com:8080

指定请求头 curl -H "user-agent: xxx" example.com

多个网络接口和 IP 时,指定其中一个

sh
curl --interface enp1s0:1 example.com

curl --interface 123.123.123.123 example.com

下载文件 curl -O index.html example.com

跟随重定向 curl -L https://example.com

POST 请求

POST 方式提交 JSON 文件:

shell
echo '{"name":"foo","age":123}' > /tmp/test.json

curl -H "Content-Type: application/json" \
-XPOST \
-d @/tmp/test.json \
httpbin.org/post

请求体类似

txt
POST / HTTP/1.1
User-Agent: curl/7.87.0
Accept: */*
Content-Type: application/json
Content-Length: 24

{"name":"foo","age":123}

仿 HTML 表单 <form method="get"> 方式提交数据:

curl --data-urlencode "name=some one&gender=female" httpbin.org/post

请求体类似

txt
POST /post HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=some one&gender=female

仿 <form method="post"> 方式提交:

curl -F 'myfile=@/tmp/hello.txt;type=application/json' -F "name=some one" httpbin.org/post

请求体类似

txt
POST /post HTTP/1.1
Content-Type: multipart/form-data; boundary=------------------------随机生成分隔符xxx


------------------------随机生成分隔符xxx
Content-Disposition: form-data; name="name"

some one
------------------------随机生成分隔符xxx
Content-Disposition: form-data; name="myfile"; filename="hello.txt"
Content-Type: application/json

hello.txt文件的内容

参数说明:

  • -F 参数可指定多个;
  • -F 参数值如果带 @ 前缀表示指向本地文件;可指定提交文件的 content-type,如上例为 application/json,不指定则默认为 application/octet-stream

指定认证

HTTP Basic 认证的域名 curl -u user:passwd https://example.com

使用需要认证的 HTTPS 代理(注:参数 u 大小写) curl -U user:passwd -x localhost:7890 https://example.com

通过 JWT 认证头 curl -H "authorization: Bearer eyJhbGc..." https://example.com

通过特定 cookie 头 curl -H "cookie: xxx=..." http://example.com

使用代理请求

使用 HTTPS 代理 curl -x localhost:7890 https://example.com

使用 SOCKS5 代理 curl --socks5 localhost:1080 https://example.com

使用 SOCKS5 代理并且让代理解释域名(避免国内电信运营商干扰) curl --socks5-hostname localhost:1080 https://example.com

通过设置 HTTPS_PROXY 环境变量

sh
https_proxy=http://localhost:7890 curl -v example.com

# 或,修改 shell 中所有新启动子进程的环境变量
export https_proxy=http://localhost:7890
curl -v example.com

HTTPS 请求

发送 HTTPS 请求,并且不校验服务端证书 curl -k https://example.com:123

发送 HTTPS 请求,并且指定服务端证书 curl --cacert server.crt https://example.com:123

校验 SSL 证书是否有效 openssl s_client -connect example.com:123

Released under the CC-BY-NC-4.0