跳转至

使用curl命令的技巧

-A 用户代理

也可以通过 -H 参数直接指定标头更改 User-Agent 配置

通过 -A 参数指定客户端的用户代理标头(User-Agent),而在默认的情况下 curl 的默认用户代理字符串是 curl/[version],其中 version 表示对应的版本号。

1
2
3
4
5
6
7
8
# 将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配置
$ curl -H 'User-Agent: php/1.0' https://google.com

通过 -b 参数用来向服务器发送自定义的 Cookie 信息。

1
2
3
4
5
6
7
8
9
# Cookie: foo=bar
# 会生成一个标头向服务器发送一个名为foo,且值为bar的Cookie信息
$ curl -b 'foo=bar' https://google.com

# 发送两个Cookie信息
$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com

# 读取cookies.txt本地文件的Cookie信息,并将其发送到服务器
$ curl -b cookies.txt https://www.google.com

通过 -c 参数将服务器设置的 Cookie 写入一个文件

# 将服务器的HTTP回应所设置Cookie写入cookies.txt文本文件
$ curl -c cookies.txt https://www.google.com

-d 发送 POST 请求的数据体

--data-urlencode等同于-d 参数,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码

通过 -d 参数用于发送 POST 请求的数据体。使用 -d 参数以后,HTTP 请求会自动加上标头 Content-Type: application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略 -X POST 参数。

1
2
3
4
5
6
# 发送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

# 读取data.txt文件的内容,作为数据体向服务器发送
$ curl -d '@data.txt' https://google.com/login
# 发送的数据hello world之间有一个空格,需要进行URL编码
$ curl --data-urlencode 'comment=hello world' https://google.com/login

-e 设置 HTTP 的标头 Referer 字段

通过 -e 参数用来设置 HTTP 的标头 Referer,表示请求的来源。

1
2
3
4
5
# 将Referer标头设为https://google.com?q=example
$ curl -e 'https://google.com?q=example' https://www.example.com

# 通过-H参数可以通过直接添加标头Referer达到同样效果
$ curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F 向服务器上传二进制文件

通过 -F 参数用来向服务器上传二进制文件。

1
2
3
# 下面命令会给HTTP请求加上标头Content-Type: multipart/form-data
# 然后将文件photo.png作为file字段上传,类似于postman中的上传文件一样
$ curl -F '[[emailprotected]](/cdn-cgi/l/email-protection)' https://google.com/profile

通过 -F 参数可以指定 MIME 类型。

1
2
3
# 指定MIME类型为image/png
# 否则curl会把MIME类型设为application/octet-stream
$ curl -F '[[emailprotected]](/cdn-cgi/l/email-protection);type=image/png' https://google.com/profile

通过 -F 参数也可以指定文件名。

# 原始文件名为photo.png,但是服务器接收到的文件名为me.png
$ curl -F '[[emailprotected]](/cdn-cgi/l/email-protection);filename=me.png' https://google.com/profile

-G 构造 URL 的查询字符串

通过 -G 参数用来构造 URL 的查询字符串。

1
2
3
4
5
6
# 下面命令会发出一个GET请求,如果省略-G参数会发出一个POST请求
# 实际请求的URL地址为 => https://google.com/search?q=kitties& count=20
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

# 如果数据需要URL编码,可以结合--data--urlencode参数
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H 添加 HTTP 请求的标头

通过 -H 参数添加 HTTP 请求的标头。

1
2
3
4
5
6
7
8
9
# 添加HTTP标头 Accept-Language: en-US
$ curl -H 'Accept-Language: en-US' https://google.com

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

# 添加 HTTP 请求的标头是Content-Type: application/json
# 然后通过-d参数发送JSON数据发送到服务器上面
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

-i 打印服务器回应的 HTTP 标头

通过 -i 参数打印出服务器回应的 HTTP 标头。

# 下面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码
$ curl -i https://www.example.com

-I 打印服务器回应的 HEAD 标头

通过 -I 参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

1
2
3
4
5
# 输出服务器对HEAD请求的回应
$ curl -I https://www.example.com

# --head参数等同于-I参数
$ curl --head https://www.example.com

-k 跳过 SSL 检测

通过 -k 参数指定跳过 SSL 检测。

# 不会检查服务器的SSL证书是否正确
$ curl -k https://www.example.com

-L 跟随服务器的重定向

通过 -L 参数会让 HTTP 请求跟随服务器的重定向,而 curl 默认不跟随重定向。

# 请求跟随服务器的重定向
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

–limit-rate 限制请求和回应的带宽

通过 --limit-rate 用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

# 将带宽限制在每秒200K字节
$ curl --limit-rate 200k https://google.com

-o 将服务器的回应保存成文件

通过 -o 参数将服务器的回应保存成文件,等同于 wget 命令。

# 将www.example.com保存成example.html文件
$ curl -o example.html https://www.example.com

-O 将服务器的回应保存成文件

通过 -O 参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

# 将服务器回应保存成文件,文件名为bar.html
$ curl -O https://www.example.com/foo/bar.html

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

通过 -s 参数将不输出错误和进度信息。

1
2
3
4
5
# 一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果
$ curl -s https://www.example.com

# 想让curl不产生任何输出,可以使用下面的命令
$ curl -s -o /dev/null https://google.com

-S 只输出错误信息

通过 -S 参数指定只输出错误信息,通常与 -s 一起使用。

# 没有任何输出,除非发生错误
$ curl -s -o /dev/null https://google.com

-u 设置认证的用户名和密码

通过 -u 参数用来设置服务器认证的用户名和密码。

1
2
3
4
5
6
7
8
9
# 设置用户名为bob,密码为12345
# 然后将其转为HTTP标头 Authorization: Basic Ym9iOjEyMzQ1
$ curl -u 'bob:12345' https://google.com/login

# curl 能够识别URL里面的用户名和密码
$ curl https://bob:[[emailprotected]](/cdn-cgi/l/email-protection)/login

# 只设置了用户名,执行后curl会提示用户输入密码
$ curl -u 'bob' https://google.com/login

-v 打印调试信息

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

1
2
3
4
5
# 打印调试信息
$ curl -v https://www.example.com

# --trace参数也可以用于调试,还会输出原始的二进制数据。
$ curl --trace - https://www.example.com

-x 设置请求代理

通过 -x 参数指定 HTTP 请求的代理。

1
2
3
4
5
# 指定HTTP请求通过myproxy.com:8080的socks5代理发出
$ curl -x socks5://james:[[emailprotected]](/cdn-cgi/l/email-protection):8080 https://www.example.com

# 如果没有指定代理协议,默认为 HTTP
$ curl -x james:[[emailprotected]](/cdn-cgi/l/email-protection):8080 https://www.example.com

-X 指定请求的方法

通过 -X 参数指定 HTTP 请求的方法。

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