跳转至

curl⚓︎

618 个字 65 行代码 预计阅读时间 4 分钟

参考资料

curl 意为客户端 (client) URL 工具,是一种命令行工具,用来请求 Web 服务器。以下是 curl 的一些可用参数:

注意

下面涉及到的 URL 不一定是真实的 URL,有些是随便杜撰出来的 ......

-A

指定客户端的用户代理 ( User-Agent ) 标头,默认用户代理字符串是 curl/[version]

# 将用户代理改为 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://example.com

# 移除用户代理标头
$ curl -A '' https://example.com

-b

指定要发送的 Cookie

$ curl -b 'foo=bar' https://example.com
  • 可以发送多个 Cookie,中间用分号间隔
$ curl -b 'foo1=bar1;foo2=bar2' https://example.com
  • 也可以发送带 Cookie 的文件
$ curl -b cookies.txt https://example.com

-c

将服务器设置的 Cookie 写入一个文件内

$ curl -c cookies.txt https://example.com

-d / --data-urlencode

发送 POST 请求的数据体

  • 此时会自动加上标头 Content-Type: application/x-www-form-urlencoded
  • 并且将请求方法自动设置为 POST,因此下面的 -X POST 可省略
  • 两者的唯一区别在于后者会将数据进行 URL 编码
$ curl -d 'login=name&password=123456' -X POST https://example.com/login

# 或者
$ curl -d 'login=name' -d 'password=123456' -X POST https://example.com/login

# 读取文件(data.txt)
$ curl -d '@data.txt' https://example.com/login

# 数据 'hello world' 中的空格会转为 URL 编码 %20
$ curl --data-urlencode 'comment=hello world' https://example.com/comments

-e

设置 HTTP 标头 Referer(请求来源)

curl -e 'https://src.com?q=example' https://dst.com

-F

上传二进制文件(图片、音频、视频等文件)

# 上传文件 photo.png
$ curl -F '[email protected]' https://blog.com/profile
  • 此时会自动添加标头 Content-Type: multipart/form-data
  • 也可以指定 MIME 类型(不设置的话默认为 application/octet-stream
# MIME 类型为 image/png
$ curl -F '[email protected];type=image/png' https://blog.com/profile
  • 还可以指定服务器接收到的文件名
# 原始文件名为 photo.png,服务器接收到的文件名为 picture.png
$ curl -F '[email protected];filename=picture.png' https://blog.com/profile

-G

构造 URL 查询字符串

# 实际请求的 URL 为 https://example.com/search?name=noughtq&age=20
# 若省略 -G 则会发出 POST 请求
$ curl -G -d 'name=noughtq' -d 'age=20' https://example.com/search

# 若数据需要 URL 编码,用 --data-urlencode 替代 -d

-H

添加指定的 HTTP 请求标头

$ curl -H 'Accept-Language: en-US' https://example.com

# 添加多个标头
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://example.com

# 组合技:发送 JSON 数据
$ curl -d '{"login": "noughtq", "pass": "123456"}' -H 'Content-Type: application/json' https://example.com/login

-i

额外打印 HTTP 响应标头(先输出标头,空一行再输出网页源码)

$ curl -i https://example.com

-I / --head

仅打印 HTTP 响应标头

$ curl -i https://example.com
# 或者
$ curl --head https://example.com

-k

跳过 SSL 检测

$ curl -k https://example.com

-L

HTTP 请求跟踪服务器的重定向(默认不跟踪)

$ curl -L https://example.com

--limit-rate

限制 HTTP 请求和回应的带宽,可用于模拟慢网速的环境

# 带宽限制为 200 KB/s
$ curl --limit-rate 200k https://example.com

-o

将服务器的响应内容保存在指定文件内(等同于 wget 命令)

$ curl -o example.html https://example.com

-O

类似上一条指令,但是将 URL 的最后部分作为文件名

# 服务器响应内容保存在文件 bar.html 内
$ curl -O https://example.com/foo/bar.html

-s

不输出错误和进度信息,不发生错误的话会正常显示运行结果

$ curl -s https://example.com

# 不产生任何输出
$ curl -s -o /dev/null https://example.com

-S

仅输出错误信息,通常与 -s 一起使用

$ curl -sS -o /dev/null https://example.com

-u

设置服务器认证的用户名和密码

$ curl -u 'noughtq:123456' https://example.com/login
  • 事实上,curl 可以自动识别 URL 中的用户名和密码
# 等价于上一条命令
$ curl https://noughtq:[email protected]/login
  • 可以仅设置用户名,但 curl 会提示输入密码
$ curl -u 'noughtq' https://example.com/login
Enter host password for user 'noughtq':

-v / --trace

输出通信的全过程,用于调试

$ curl -v https://example.com

--trace 还会输出原始的二进制数据

$ curl --trace https://example.com

-x

指定 HTTP 请求代理(若未指定,默认为 HTTP

# 该请求通过 myproxy.com:8080 的 socks5 代理发出
$ curl -x socks5://james:[email protected]:8080 https://example.com

-X

指定 HTTP 请求方法

# 发出 POST 请求
$ curl -X POST https://example.com

评论区

如果有什么问题或想法,欢迎大家在下方留言~