Curl 备忘清单包含命令和一些常见的 Curl 技巧示例。

入门

介绍

Curl 是一种在服务器之间传输数据的工具,支持协议,包括 HTTP/FTP/IMAP/LDAP/POP3/SCP/SFTP/SMB/SMTP 等

Options

1
2
-o <file>    # --output: 写入文件
-u user:pass # --user: 验证

1
2
3
4
-v           # --verbose: 在操作期间使 curl 冗长
-vv # 更冗长
-s # --silent: 不显示进度表或错误
-S # --show-error: 与 --silent (-sS) 一起使用时,显示错误但没有进度表

1
2
-i           # --include: 在输出中包含 HTTP 标头
-I # --head: 仅标头

请求

1
2
3
-X POST # --request
-L # 如果页面重定向,请点击链接
-F # --form: multipart/form-data 的 HTTP POST 数据

数据

1
2
3
4
5
6
7
8
9
# --data: HTTP post 数据
# URL 编码(例如,status="Hello")
-d 'data'

# --data 通过文件
-d @file

# --get: 通过 get 发送 -d 数据
-G

头信息 Headers

1
2
3
4
5
6
7
8
9
10
11
-A <str>     # --user-agent
-b name=val # --cookie

# 从 URL 的指定文件加载 cookie
-b, --cookie FILE
# 将 cookie 从 URL 保存到指定文件
-c, --cookie-jar FILE

-b FILE # --cookie
-H "X-Foo: y" # --header
--compressed # 使用 deflate/gzip

SSL

1
2
--cacert <file>
--capath <dir>
1
2
3
-E, --cert <cert>     # --cert: 客户端证书文件
--cert-type # der/pem/eng
-k, --insecure # 对于自签名证书

安装

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 获取带有标题的响应

多文件上传

1
2
3
$ curl -v -include \
--form key1=value1 \
--form upload=@localfilename URL

为 curl 响应美化 json 输出

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

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 上传

检查网站响应时间

1
2
3
curl -s -w \
'\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' \
-o /dev/null https://www.google.com

使用 Curl 检查远程资源是否可用

1
curl -o /dev/null --silent -Iw "%{http_code}" https://example.com/my.remote.tarball.gz

正在下载文件

1
2
3
4
5
curl https://example.com | \
grep --only-matching 'src="[^"]*.[png]"' | \
cut -d\" -f2 | \
while read i; do curl https://example.com/"${i}" \
-o "${i##*/}"; done

从站点下载所有 PNG 文件(使用GNU grep)

下载文件,保存文件而不更改其名称

1
curl --remote-name "https://example.com/linux-distro.iso"

重命名文件

1
curl --remote-name "http://example.com/index.html" --output foo.html

继续部分下载

1
curl --remote-name --continue-at - "https://example.com/linux-distro.iso"

从多个域下载文件

1
curl "https://www.{example,w3,iana}.org/index.html" --output "file_#1.html"

下载一系列文件

1
curl "https://{foo,bar}.com/file_[1-4].webp" --output "#1_#2.webp"

下载一系列文件(输出foo_file1.webpfoo_file2.webp…bar_file1_webp等)

将输出重定向到文件

1
$ curl http://url/file > file

基本认证

1
2
$ curl --user username:password http://example.com/
$ curl -u username:password http://example.com/

写入文件而不是标准输出

1
2
$ curl -o file http://url/file
$ curl --output file http://url/file

下载头信息

1
2
$ curl -I url
# 显示头信息

将输出写入名为远程文件的文件

1
2
$ curl -o file http://url/file
$ curl --output file http://url/file

执行远程脚本

1
$ curl -s http://url/myscript.sh

配置文件

1
2
3
4
curl -K file
# 从文件中读取配置
curl --config file
$HOME/.curlrc # 类 UNIX 系统中的默认配置文件

评论
avatar
竹山一叶
技术分享 个人心得
Follow Me
公告
欢迎光临小站,这里是我日常工作和学习中收集和整理的总结,希望能对你有所帮助:)

本站的内容经过个人加工总结而来,也参考了网友们分享的资料,如有侵权,请第一时间联系我,我将及时进行修改或删除😊
最新文章
网站资讯
文章数目 :
436
已运行时间 :
本站总字数 :
431.5k
本站访客数 :
本站总访问量 :
最后更新时间 :
文章归档文章分类文章标签复制本文标题复制本文地址
随便逛逛