curl命令指南

2,607 阅读3分钟

平时Postman用的比较多,它可以很方便的生成curl命令,很多时候我们在服务器上调试并没有Postman的时候,就需要curl,一个非常强大的命令,整理一下,已备搬砖之需。

curl命令格式的格式:

curl [options...] <url>

一、POST请求

  • 空请求体
curl -X POST https://catonmat.net
  • Form格式

在有 -d 参数的情况下,默认为POST请求,-X参数可以去掉;Content-Type 默认为:application/x-www-form-urlencoded。

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

或者

curl -d 'login=emma' -d 'password=123' https://google.com/login
  • 重定向

添加 -L 参数对3开头的应答进行重定向请求,默认情况下,curl不会进行重定向。

curl -L -d 'tweet=hi' https://api.twitter.com/tweet
  • JSON格式

添加 -H 'Content-Type: application/json'

curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
  • XML格式

添加 -H 'Content-Type: text/xml'

curl -d '<user><login>ann</login><password>123</password></user>' -H 'Content-Type: text/xml' https://google.com/login
  • 文本格式

添加 -H 'Content-Type: text/plain'

curl -d 'hello world' -H 'Content-Type: text/plain' https://google.com/login
  • 请求参数来自文件
curl -d '@data.txt' https://google.com/login
  • URL-encode编码
curl --data-urlencode 'comment=hello world' https://google.com/login
  • 发送二进制数据

添加 -F 参数,支持Multipart数据,比如图片或者文件

curl -F 'file=@photo.png' https://google.com/profile
  • 指定MIME类型

如果不指定MIME类型,默认类型为:application/octet-stream

curl -F 'file=@photo.png;type=image/png' https://google.com/profile
  • 改变发送文件的名称
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

二、查询请求

  • 使用 -G 参数添加查询参数
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

最终格式:

https://google.com/search?q=kitties&count=20
  • 使用URL-encode对查询参数进行编码
curl -G --data-urlencode 'comment=this cookbook is awesome' https://catonmat.net

最终格式:

https://catonmat.net?comment=this%20cookbook%20is%20awesome

三、HTTP Header 设置

  • 使用-H添加请求头
curl -H 'Accept-Language: en-US' https://google.com
  • 添加空请求头

使用-H参数:转化"Puppies;"为"Puppies:",没有值

curl -H 'Puppies;' https://google.com

四、User Agent 设置

  • 使用-A设置User Agent
curl -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0' https://google.com
  • 移除User Agent
curl -A '' https://google.com
  • 使用-H设置User Agent
curl -H 'User-Agent: php/1.0' https://google.com
  • 发送空的User Agent

使用-A ''去掉原来的User Agent,然后使用-H 'User-Agent;',设置空的User Agent。

curl -A '' -H 'User-Agent;' https://google.com

五、Cookie 设置

默认 curl 命令不会添加任何Cookie。

  • 使用 -b 设置Cookie
curl -b 'session=abcdef' https://google.com
  • 设置空Cookie
curl -b 'session=' https://google.com
  • 保存Cookie到文件
curl -c cookies.txt https://www.google.com
  • 从文件中加载Cookie
curl -b cookies.txt https://www.google.com

六、Referrer 设置

HTTP参照地址,用来表示从哪连接到当前网页。

  • 添加Referrer
curl -e 'https://google.com?q=cats' http://catonmat.net
  • 发送空Referrer
curl -e '' http://catonmat.net
  • 使用-H添加Referrer
curl -H 'Referer: https://digg.com' http://catonmat.net

七、Basic HTTP 授权请求

使用 -u user:pass

  • 用户名/密码授权请求
curl -u 'bob:12345' https://google.com/login

或者

curl https://bob:12345@google.com/login
  • 设置空密码
curl -u 'bob:' https://google.com/login
  • 提示输入密码
curl -u 'bob' https://google.com/login

八、代理设置

使用-x添加代理

  • 设置Socks5代理
curl -x socks5://james:cats@myproxy.com:8080 https://catonmat.net
  • 设置Socks4代理
curl -x socks4://james:cats@myproxy.com:8080 https://catonmat.net
  • 这只HTTP代理
curl -x james:cats@myproxy.com:8080 https://catonmat.net
  • 部分使用代理

https://host.com 不使用代理

https://catonmat.net https://digg.com 使用代理https://myproxy.com:8080

curl --no-proxy host.com -x https://myproxy.com:8080 host.com https://catonmat.net https://digg.com https://host.com

九、SSH 验证

  • 忽略 SSH 验证
curl -k https://catonmat.net
  • 使用 SSH 验证
# SSLv1
curl -1 https://catonmat.net
# SSLv2
curl -2 https://catonmat.net
# SSLv3
curl -3 https://catonmat.net

十、HTTP请求限速

  • 限速 200 K/s
curl --limit-rate 200k https://google.com
# megabytes
curl --limit-rate 200m https://google.com
# gigabytes
curl --limit-rate 200g https://google.com
  • 限速 1 Byte/s
curl --limit-rate 1 https://google.com

HTTP 调试

  • 打印Verbose
curl -v https://catonmat.net
  • 打印Detailed Trace
curl --trace - https://catonmat.net
  • 打印带时间戳的Detailed Trace
curl --trace - --trace-time https://catonmat.net
  • 打印 Response Headers 和 Response Body
curl -i https://catonmat.net
  • 只打印 Response Headers

-s:安静输出

-o /dev/null:丢弃Response Body

-D - :将输出保存到文件,-表示标准输出

curl -s -o /dev/null -D - https://catonmat.net
  • 只打印 Response Code
curl -w '%{response_code}' -s -o /dev/null https://catonmat.net
  • 只打印 Request Headers
curl -v -s -o /dev/null --stderr - https://catonmat.net | grep '^>'

参考