非常值得一看的 Curl 用法指南

非常值得一看的 Curl 用法指南
最新回答
良辰未赏透

2023-06-24 09:21:55

Curl 是一个功能强大的命令行工具,用于请求 Web 服务器,可替代图形界面工具如 Postman。其常用命令行参数及用法如下

  • 基础请求

    不带参数时,默认发出 GET 请求,如:curl

    https://www.example.com
    ,服务器返回内容在命令行输出。

  • 请求头相关

    -A:指定客户端用户代理标头(User-Agent)。

    修改为 Chrome 浏览器:curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'

    https://google.com

    移除 User-Agent 标头:curl -A ''

    https://google.com

    -H:直接指定标头,可更改 User-Agent 或添加其他标头。

    更改 User-Agent:curl -H 'User-Agent: php/1.0'

    https://google.com

    添加多个标头:curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy'

    https://google.com

    发送 JSON 数据时指定标头:curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json'

    https://google.com/login

    -e:设置 HTTP 标头 Referer,表示请求来源。

    设置 Referer 标头:curl -e '

    https://google.com?q=example'
    https://www.example.com

    也可通过 -H 参数实现:curl -H 'Referer:

    https://google.com?q=example'
    https://www.example.com

  • Cookie 相关

    -b:向服务器发送 Cookie。

    发送单个 Cookie:curl -b 'foo=bar'

    https://google.com

    发送多个 Cookie:curl -b 'foo1=bar' -b 'foo2=baz'

    https://google.com

    读取本地文件发送 Cookie:curl -b cookies.txt

    https://www.google.com

    -c:将服务器设置的 Cookie 写入文件。

    写入 Cookie 到文件:curl -c cookies.txt

    https://www.google.com

  • 请求方法与数据体

    -d:发送 POST 请求的数据体,自动添加 Content-Type: application/x-www-form-urlencoded 标头,并转为 POST 方法(可省略 -X POST)。

    发送数据:curl -d'login=emma&password=123' -X POST

    https://google.com/login
    或 curl -d 'login=emma' -d 'password=123' -X POST
    https://google.com/login

    读取本地文件发送数据:curl -d '@data.txt'

    https://google.com/login

    --data-urlencode:等同于 -d,但会自动对发送数据进行 URL 编码。

    发送需编码的数据:curl --data-urlencode 'comment=hello world'

    https://google.com/login

    -X:指定 HTTP 请求方法。

    发出 POST 请求:curl -X POST

    https://www.example.com

  • 文件上传

    -F:向服务器上传二进制文件,自动添加 Content-Type: multipart/form-data 标头。

    上传文件:curl -F 'file=@photo.png'

    https://google.com/profile

    指定 MIME 类型:curl -F 'file=@photo.png;type=image/png'

    https://google.com/profile

    指定文件名:curl -F 'file=@photo.png;filename=me.png'

    https://google.com/profile

  • URL 构造与查询

    -G:构造 URL 的查询字符串,发出 GET 请求。

    构造查询字符串:curl -G -d 'q=kitties' -d 'count=20'

    https://google.com/search

    结合 URL 编码:curl -G --data-urlencode 'comment=hello world'

    https://www.example.com

  • 响应处理

    -i:打印服务器回应的 HTTP 标头及网页源码。

    打印标头和源码:curl -i

    https://www.example.com

    -I(或 --head):发出 HEAD 请求,打印服务器返回的 HTTP 标头。

    输出 HEAD 请求回应:curl -I

    https://www.example.com

    -o:将服务器回应保存成文件,等同于 wget 命令。

    保存为指定文件名:curl -o example.html

    https://www.example.com

    -O:将服务器回应保存成文件,以 URL 最后部分作为文件名。

    保存为 URL 最后部分文件名:curl -O

    https://www.example.com/foo/bar.html

  • 调试与输出控制

    -v:输出通信的整个过程,用于调试。

    调试通信过程:curl -v

    https://www.example.com

    --trace:用于调试,输出原始的二进制数据。

    调试并输出二进制数据:curl --trace -

    https://www.example.com

    -s:不输出错误和进度信息。

    不显示错误信息:curl -s

    https://www.example.com

    不产生任何输出:curl -s -o /dev/null

    https://google.com

    -S:只输出错误信息,通常与 -o 一起使用。

    只显示错误信息:curl -s -o /dev/null

    https://google.com

  • 其他功能

    -k:跳过 SSL 检测。

    不检查 SSL 证书:curl -k

    https://www.example.com

    -L:跟随服务器的重定向。

    跟随重定向:curl -L -d 'tweet=hi'

    https://api.twitter.com/tweet

    --limit-rate:限制 HTTP 请求和回应的带宽。

    限制带宽:curl --limit-rate 200k

    https://google.com

    -u:设置服务器认证的用户名和密码。

    设置用户名和密码:curl -u 'bob:12345'

    https://google.com/login

    识别 URL 中的用户名和密码:curl

    https://bob:12345@google.com/login

    只设置用户名:curl -u 'bob'

    https://google.com/login

    -x:指定 HTTP 请求的代理。

    指定 socks5 代理:curl -x socks5://james:cats@myproxy.com:8080

    https://www.example.com

    指定 HTTP 代理:curl -x james:cats@myproxy.com:8080

    https://www.example.com