Curl 命令
本文最后更新于:2024年3月18日 凌晨
Curl 命令
URL 访问
实例
查看详细信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| curl http://youtube.com -v * Trying 127.0.0.1... * TCP_NODELAY set * SOCKS5 communication to youtube.com:80 * SOCKS5 connect to IPv4 118.107.180.216 (locally resolved) * SOCKS5 request granted. * Connected to 127.0.0.1 (127.0.0.1) port 7890 (#0) > GET / HTTP/1.1 > Host: youtube.com > User-Agent: curl/7.64.1 > Accept: */* > * Recv failure: Connection reset by peer * Closing connection 0 curl: (56) Recv failure: Connection reset by peer
|
保存到文件
- 如果需要将页面源码保存到本地,可以使用
-o
参数:
- -o 将文件保存到 -o 指定的文件名。
- -O 将文件保存到默认文件名。
1
| curl -o google.html www.google.com
|
重定向
- 默认情况下 CURL 不会发送 HTTP Location headers(重定向). 当一个被请求页面移动到另一个站点时,会发送一个 HTTP Loaction header 作为请求,然后将请求重定向到新的地址上。
断点续传
下载脚本并执行
1
| curl -sSL http://to.sh | bash
|
- 使用
-i
或者 --include
参数查看返回 Header
- 使用
-i
参数,页面相应头 header 和页面相应 body 一起返回,如果只想查看 header,可以使用 -I
或者 --head
表单提交
- GET 提交直接将参数添加到 URL 后。
- POST 提交时。
1
| curl -X POST --data 'keyword=value' https://httpbin.org/post
|
1 2 3
| curl -X DELETE url
curl -X PUT --data 'key=value' url
|
文件上传
HTTPS 支持
1
| curl -E mycert.pem https://url
|
添加请求头
1
| curl -H ‘Content-Type:application/json' -H 'Authorization: bearer valid_token' URL
|
Cookie
-c
参数保存请求返回 Cookie,本地存储文件。
1
| curl -b cook_file.txt -c response_cookie.txt URL
|
上传 FTP
- 通过
-T
选项可指定本地文件上传到 FTP 服务器。
1 2
| curl -u ftpuser:ftppassword -T file.txt ftp://ftp.server curl -u ftpuser:ftppassword -T "{file1, file2}" ftp://ftp.server
|