curl命令的全部选项
基本选项
-o, --output <文件>: 将输出写入文件,而不是标准输出。
-O, --remote-name: 将输出写入文件,文件名从URL中获取。
-s, --silent: 静默模式,不输出任何东西。
-S, --show-error: 在静默模式下仍然显示错误信息。
-i, --include: 输出时包含HTTP头。
-I, --head: 只获取HTTP头,不获取内容。
-X, --request <方法>: 指定HTTP请求方法(如GET、POST、PUT等)。
-d, --data <数据>: HTTP POST数据。
-H, --header <头信息>: 添加HTTP头。
-u, --user [用户名:密码]: 设置服务器的用户和密码。
-L, --location: 跟随HTTP重定向。
-v, --verbose: 输出更多的信息,用于调试。
其他常用选项
-A, --user-agent <string>: 设置用户代理发送给服务器。
-b, --cookie <name=string/file>: 设置cookie字符串或文件读取位置。
-c, --cookie-jar <file>: 操作结束后把cookie写入到这个文件中。
-D, --dump-header <file>: 把header信息写入到该文件中。
-e, --referer <URL>: 设置来源网址。
-f, --fail: 如果服务器返回的是大于或等于400的HTTP状态码,curl会以非零退出状态退出。
-T, --upload-file <file>: 上传文件。
-k, --insecure: 允许不安全的SSL连接和传输。
-r, --range <range>: 检索来自HTTP/1.1或FTP服务器字节范围。
-x, --proxy <host[:port]>: 在给定的端口上使用HTTP代理。
-#, --progress-bar: 进度条显示当前的传送状态。
示例用法
获取网页内容:
curl http://example.com
将网页内容保存到文件:
curl -o example.html http://example.com
发送POST请求:
curl -d "param1=value1¶m2=value2" -X POST http://example.com/resource
发送带有头部的POST请求:
curl -H "Content-Type: application/json" -X POST -d '{"key1":"value1", "key2":"value2"}' http://example.com/resource下载文件:
curl -O http://example.com/file.tar.gz
显示响应头:
curl -I http://example.com
跟随重定向:
curl -L http://example.com/redirect
使用用户名和密码进行认证:
curl -u username:password http://example.com
