Curl
此 Curl 备忘清单包含命令和一些常见的 Curl 技巧示例。
入门
介绍
Curl 是一种在服务器之间传输数据的工具,支持协议,包括 HTTP/FTP/IMAP/LDAP/POP3/SCP/SFTP/SMB/SMTP 等
- Curl GitHub 源码仓库 (github.com)
- Curl 官方网站 (curl.se)
Options
BASH
1 | -o <file> # --output: 写入文件 |
BASH
1 | -v # --verbose: 在操作期间使 curl 冗长 |
BASH
1 | -i # --include: 在输出中包含 HTTP 标头 |
请求
BASH
1 | -X POST # --request |
数据
BASH
1 | # --data: HTTP post 数据 |
头信息 Headers
BASH
1 | -A <str> # --user-agent |
SSL
BASH
1 | --cacert <file> |
BASH
1 | -E, --cert <cert> # --cert: 客户端证书文件 |
安装
BASH
1 | apk add --update curl # alpine linux 中安装 |
示例
CURL GET/HEAD
| 命令 | 说明 |
|---|---|
curl -I https://www.baidu.com |
curl 发请求 |
curl -v -I https://www.baidu.com |
带有详细信息的 curl 发请求 |
curl -X GET https://www.baidu.com |
使用显式 http 方法进行 curl |
curl --noproxy 127.0.0.1 http://www.stackoverflow.com |
没有 http 代理的 curl |
curl --connect-timeout 10 -I -k https://www.baidu.com |
curl 默认没有超时 |
curl --verbose --header "Host: www.mytest.com:8182" www.baidu.com |
curl 得到额外的标题 |
curl -k -v https://www.google.com |
curl 获取带有标题的响应 |
多文件上传
BASH
1 | $ curl -v -include \ |
为 curl 响应美化 json 输出
BASH
1 | $ curl -XGET http://${elasticsearch_ip}:9200/_cluster/nodes | python -m json.tool |
CURL POST
| 命令 | 说明 |
|---|---|
curl -d "name=username&password=123456" <URL> |
curl 发请求 |
curl <URL> -H "content-type: application/json" -d "{ \"woof\": \"bark\"}" |
curl 发送 json |
CURL 脚本安装 rvm
BASH
1 | curl -sSL https://get.rvm.io | bash |
CURL 高级
| 命令 | 说明 |
|---|---|
curl -L -s http://ipecho.net/plain, curl -L -s http://whatismijnip.nl |
获取我的公共 IP |
curl -u $username:$password http://repo.dennyzhang.com/README.txt |
带凭证的 curl |
curl -v -F key1=value1 -F upload=@localfilename <URL> |
curl 上传 |
curl -k -v --http2 https://www.google.com/ |
使用 http2 curl |
curl -T cryptopp552.zip -u test:test ftp://10.32.99.187/ |
curl ftp 上传 |
curl -u test:test ftp://10.32.99.187/cryptopp552.zip -o cryptopp552.zip |
curl ftp 下载 |
curl -v -u admin:admin123 --upload-file package1.zip http://mysever:8081/dir/package1.zip |
使用凭证 curl 上传 |
检查网站响应时间
BASH
1 | curl -s -w \ |
使用 Curl 检查远程资源是否可用
BASH
1 | curl -o /dev/null --silent -Iw "%{http_code}" https://example.com/my.remote.tarball.gz |
正在下载文件
BASH
1 | curl https://example.com | \ |
从站点下载所有 PNG 文件(使用GNU grep)
下载文件,保存文件而不更改其名称
BASH
1 | curl --remote-name "https://example.com/linux-distro.iso" |
重命名文件
BASH
1 | curl --remote-name "http://example.com/index.html" --output foo.html |
继续部分下载
BASH
1 | curl --remote-name --continue-at - "https://example.com/linux-distro.iso" |
从多个域下载文件
BASH
1 | curl "https://www.{example,w3,iana}.org/index.html" --output "file_#1.html" |
下载一系列文件
BASH
1 | curl "https://{foo,bar}.com/file_[1-4].webp" --output "#1_#2.webp" |
下载一系列文件(输出foo_file1.webp、foo_file2.webp…bar_file1_webp等)
将输出重定向到文件
BASH
1 | $ curl http://url/file > file |
基本认证
BASH
1 | $ curl --user username:password http://example.com/ |
写入文件而不是标准输出
BASH
1 | $ curl -o file http://url/file |
下载头信息
BASH
1 | $ curl -I url |
将输出写入名为远程文件的文件
BASH
1 | $ curl -o file http://url/file |
执行远程脚本
BASH
1 | $ curl -s http://url/myscript.sh |
配置文件
BASH
1 | curl -K file |