curl 是一种用于与 Web 服务器进行交互的命令行工具,它支持多种协议(HTTP、FTP、SMTP、POP3 等)以及各种选项和参数。

常用的 curl 命令行参数
-X, --request: 设置 HTTP 请求方法,如 GET、POST、PUT、DELETE 等
-H, --header: 设置请求头,如 "Content-Type: application/json"
-d, --data: 设置请求体,如 "{'username': 'user', 'password': 'pass'}"
-i, --include: 在输出中包含响应头信息
-o, --output: 将响应体保存到文件中
-L, --location: 自动跟随重定向
-u, --user: 设置 HTTP 认证用户名和密码
-s, --silent: 静默模式,不输出任何信息
-k, --insecure: 允许不安全的 SSL 连接
-A, --user-agent: 设置 User-Agent 标头
-c, --cookie: 设置 cookie,如 "session_id=123456"
-b, --cookie-jar: 指定保存 cookie 的文件
-T, --upload-file: 上传文件,如 "image.png"
-v, --verbose: 显示详细的调试信息

具体示例说明:
-X, --request: 设置 HTTP 请求方法。默认情况下,curl 使用 GET 方法发送请求。如果需要使用其他方法(如 POST、PUT、DELETE 等),可以使用此选项设置。例如:

curl -X POST http://example.com

-H, --header: 设置请求头。可以使用该选项设置请求头信息,例如设置 Content-Type。多个请求头信息可以通过多次使用该选项来设置。例如:

curl -H "Content-Type: application/json" -H "Authorization: Bearer xxx" http://example.com

-d, --data: 设置请求体。可以使用该选项设置请求体的内容,例如发送表单或 JSON 数据。例如:

curl -d "username=user&password=pass" http://example.com/login

-i, --include: 在输出中包含响应头信息。该选项可以在 curl 输出中包含响应头信息。例如:

curl -i http://example.com

-o, --output: 将响应体保存到文件中。该选项可以将响应体保存到指定的文件中。例如:

curl -o response.txt http://example.com

-L, --location: 自动跟随重定向。该选项可以自动跟随 HTTP 重定向。例如:

curl -L http://example.com

-u, --user: 设置 HTTP 认证用户名和密码。该选项可以设置 HTTP 基本认证的用户名和密码。例如:

curl -u username:password http://example.com

-s, --silent: 静默模式,不输出任何信息。该选项可以让 curl 在运行时不输出任何信息。例如:

curl -s http://example.com

-k, --insecure: 允许不安全的 SSL 连接。该选项可以让 curl 不验证 SSL 证书。请注意,这会降低安全性,只有在测试或开发环境中才应使用。例如:

curl -k https://example.com

-A, --user-agent: 设置 User-Agent 标头。该选项可以设置 HTTP 请求中的 User-Agent 标头信息。例如:

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" http://example.com

-c, --cookie: 设置 cookie。该选项可以设置 HTTP 请求中的 cookie。例如:

curl -c "session_id=123456" http://example.com

-b, --cookie-jar: 指定保存 cookie 的文件。该选项可以将服务器返回的 cookie 保存到指定的文件中。例如:

curl -b cookies.txt http://example.com

-T, --upload-file: 上传文件。该选项可以使用 HTTP PUT 方法上传文件。例如:

curl -T file.txt http://example.com/upload

-v, --verbose: 显示详细的调试信息。该选项可以在 curl 输出中显示详细的调试信息,如请求头、响应头、状态码等。例如:

curl -v http://example.com
进一步

-x, --proxy: 使用代理服务器。该选项可以使用指定的代理服务器发送请求。例如:

curl -x http://proxy.example.com:8080 http://example.com

-X, --proxy-request: 设置代理服务器的请求方法。该选项可以设置代理服务器的请求方法。例如:

curl -X CONNECT -x http://proxy.example.com:8080 http://example.com

-U, --proxy-user: 设置代理服务器的认证用户名和密码。该选项可以设置代理服务器的认证用户名和密码。例如:

curl -U username:password -x http://proxy.example.com:8080 http://example.com

-I, --head: 发送 HEAD 请求。该选项可以发送 HTTP HEAD 请求,仅返回响应头信息,而不返回响应体。例如:

curl -I http://example.com

-r, --range: 请求部分内容。该选项可以请求指定范围的内容,如文件的一部分。例如:

curl -r 0-1023 http://example.com/file.txt

-e, --referer: 设置 Referer 标头。该选项可以设置 HTTP 请求中的 Referer 标头信息。例如:

curl -e http://example.com/referer http://example.com

-H, --host: 设置 Host 标头。该选项可以设置 HTTP 请求中的 Host 标头信息。例如:

curl -H "Host: example.com" http://192.168.0.1

–retry: 设置重试次数。该选项可以设置请求失败后的重试次数。例如:

curl --retry 3 http://example.com

-w, --write-out: 自定义输出格式。该选项可以使用自定义的格式输出 curl 命令的结果。例如:

curl -w "%{http_code}\n" http://example.com

–compressed: 自动解压缩。该选项可以自动解压缩使用 gzip 或 deflate 压缩算法压缩的响应体。例如:

curl --compressed http://example.com

-F, --form: 使用表单数据。该选项可以发送包含表单数据的 POST 请求。例如:

curl -F "name=user" -F "file=@file.txt" http://example.com/upload

–form-string: 使用字符串表单数据。该选项可以发送包含字符串表单数据的 POST 请求。例如:

curl --form-string "name=user" --form-string "password=pass" http://example.com/login

–url: 设置请求的 URL。该选项可以设置请求的 URL。例如:

curl --url http://example.com

–connect-timeout: 设置连接超时时间。该选项可以设置连接超时时间。例如:

curl --connect-timeout 5 http://example.com

–max-time: 设置请求超时时间。该选项可以设置请求超时时间。例如:

curl --max-time 10 http://example.com

–limit-rate: 限制请求速度。该选项可以限制请求的速度。例如:

curl --limit-rate 100K http://example.com

–max-filesize: 设置最大下载文件大小。该选项可以设置下载文件的最大大小。例如:

curl --max-filesize 10M http://example.com/file.txt

–ipv4: 强制使用 IPv4。该选项可以强制使用 IPv4 协议。例如:

curl --ipv4 http://example.com

–ipv6: 强制使用 IPv6。该选项可以强制使用 IPv6 协议。例如:

curl --ipv6 http://example.com

-G, --get: 发送 GET 请求。该选项可以发送 GET 请求,将参数附加到 URL 上。例如:

curl -G -d "name=user" -d "password=pass" http://example.com/login

-T, --upload-file: 上传文件。该选项可以使用 HTTP PUT 方法上传文件。例如:

curl -T file.txt http://example.com/upload

-sS, --silent --show-error: 静默模式,但显示错误信息。该选项可以在静默模式下,但仍然显示错误信息。例如:

curl -sS http://example.com

-I, --head: 发送 HEAD 请求。该选项可以发送 HTTP HEAD 请求,仅返回响应头信息,而不返回响应体。例如:

curl -I http://example.com

-A, --user-agent: 设置 User-Agent 标头。该选项可以设置 HTTP 请求中的 User-Agent 标头信息。例如:

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" http://example.com

-e, --referer: 设置 Referer 标头。该选项可以设置 HTTP 请求中的 Referer 标头信息。例如:

curl -e http://example.com/referer http://example.com

-w, --write-out: 自定义输出格式。该选项可以使用自定义的格式输出 curl 命令的结果。例如:

curl -w "%{http_code}\n" http://example.com

–compressed: 自动解压缩。该选项可以自动解压缩使用 gzip 或 deflate 压缩算法压缩的响应体。例如:

curl --compressed http://example.com

-u, --user: 设置 HTTP 认证用户名和密码。该选项可以设置 HTTP 基本认证的用户名和密码。例如:

curl -u username:password http://example.com

-b, --cookie: 设置 cookie。该选项可以设置 HTTP 请求中的 cookie。例如:

curl -b "session_id=123456" http://example.com

-c, --cookie-jar: 指定保存 cookie 的文件。该选项可以将服务器返回的 cookie 保存到指定的文件中。例如:

curl -c cookies.txt http://example.com

–cookie-string: 使用 cookie 字符串。该选项可以使用 cookie 字符串设置 HTTP 请求中的 cookie。例如:

curl --cookie-string "session_id=123456" http://example.com

-H, --proxy-header: 设置代理服务器的请求头。该选项可以设置代理服务器的请求头信息。例如:

curl -H "X-Forwarded-For: 192.168.0.1" -x http://proxy.example.com:8080 http://example.com

–retry-delay: 设置重试之间的延迟时间。该选项可以设置重试之间的延迟时间。例如:

curl --retry 3 --retry-delay 5 http://example.com

–retry-max-time: 设置重试的最大时间。该选项可以设置重试的最大时间。例如:

curl --retry 3 --retry-max-time 10 http://example.com

–ftp-ssl: 使用 FTP over SSL/TLS。该选项可以使用 FTP over SSL/TLS 连接。例如:

curl --ftp-ssl ftp://example.com

-k, --config: 指定配置文件。该选项可以使用指定的配置文件运行 curl 命令。例如:

curl -k --config curl.cfg

–create-dirs: 创建目录。该选项可以在下载文件时自动创建不存在的目录。例如:

curl --create-dirs -o data/file.txt http://example.com/data/file.txt

–ftp-pasv: 使用 FTP 被动模式。该选项可以使用 FTP 被动模式连接。例如:

curl --ftp-pasv ftp://example.com

-g, --globoff: 禁用 URL 简写。该选项可以禁用 URL 简写(例如 {}[])。例如:

curl -g "http://example.com/{a,b}"

–progress-bar: 显示进度条。该选项可以在终端中显示一个进度条,显示下载或上传的进度。例如:

curl --progress-bar -o file.zip http://example.com/file.zip

-q, --disable: 静默模式。该选项可以在静默模式下运行 curl 命令。例如:

curl -q http://example.com

-t, --telnet-option: 设置 telnet 选项。该选项可以设置 telnet 协议的选项。例如:

curl -t "binary" telnet://example.com

–trace: 跟踪 HTTP 请求。该选项可以跟踪 HTTP 请求,并将跟踪信息输出到指定的文件中。例如:

curl --trace trace.txt http://example.com

–trace-ascii: 以 ASCII 格式输出跟踪信息。该选项可以将 HTTP 请求的跟踪信息以 ASCII 格式输出到指定的文件中。例如:

curl --trace-ascii trace.txt http://example.com

本文由e5pool编辑,请勿随意转载
-z, --time-cond: 设置 If-Modified-Since 头。该选项可以设置 If-Modified-Since 头,从而避免重新下载未发生更改的文件。例如:

curl -z "Fri, 19 Feb 2021 20:15:00 GMT" http://example.com/file.txt

-O, --remote-name: 使用远程文件名。该选项可以使用远程文件的名称保存下载的文件。例如:

curl -O http://example.com/file.txt

-J, --remote-header-name: 使用远程文件名(从响应头获取)。该选项可以使用响应头中的文件名保存下载的文件。例如:

curl -J -O http://example.com/download.php

–tlsv1.0, --tlsv1.1, --tlsv1.2: 强制使用指定版本的 TLS。这些选项可以强制使用指定版本的 TLS 加密协议。例如:

curl --tlsv1.2 https://example.com

–cacert: 指定 CA 证书。该选项可以使用指定的 CA 证书验证服务器证书。例如:

curl --cacert ca.pem https://example.com

–capath: 指定 CA 证书路径。该选项可以使用指定路径下的所有 CA 证书验证服务器证书。例如:

curl --capath /etc/ssl/certs https://example.com

–cert: 指定客户端证书。该选项可以使用指定的客户端证书进行身份验证。例如:

curl --cert client.pem https://example.com

–key: 指定客户端证书的密钥。该选项可以使用指定的客户端证书的密钥进行身份验证。例如:

curl --cert client.pem --key client.key https://example.com

–digest: 使用 HTTP Digest 认证。该选项可以使用 HTTP Digest 认证进行身份验证。例如:

curl --digest -u username:password http://example.com

–negotiate: 使用 Kerberos 认证。该选项可以使用 Kerberos 认证进行身份验证。例如:

curl --negotiate -u username http://example.com

–ntlm: 使用 NTLM 认证。该选项可以使用 NTLM 认证进行身份验证。例如:

curl --ntlm -u username:password http://example.com

–proxy-ntlm: 使用代理服务器的 NTLM 认证。该选项可以使用代理服务器的 NTLM 认证进行身份验证。例如:

curl --proxy-ntlm -u username:password -x http://proxy.example.com:8080 http://example.com

–krb: 使用 Kerberos 认证。该选项可以使用 Kerberos 认证进行身份验证。例如:

curl --krb -u username http://example.com

–max-redirs: 设置最大重定向次数。该选项可以设置最大重定向次数。例如:

curl --max-redirs 3 http://example.com

–anyauth: 允许使用任何身份验证方法。该选项可以允许使用任何身份验证方法进行身份验证。例如:

curl --anyauth -u username:password http://example.com

–compressed-ssh: 使用 SSH 压缩。该选项可以在 SSH 连接中使用压缩算法。例如:

curl --compressed-ssh sftp://example.com

–ftp-create-dirs: 自动创建 FTP 目录。该选项可以在 FTP 上传文件时自动创建不存在的目录。例如:

curl --ftp-create-dirs -T file.txt ftp://example.com/upload/file.txt

–ftp-method: 设置 FTP 方法。该选项可以设置使用的 FTP 方法(例如 LIST、NLST、RETR)。例如:

curl --ftp-method LIST ftp://example.com

–ftp-pret: 发送 FTP PRET 命令。该选项可以在 FTP 连接中发送 PRET 命令。例如:

curl --ftp-pret ftp://example.com

–ftp-ssl-ccc: 关闭 SSL/TLS 连接。该选项可以在 FTP 连接中关闭 SSL/TLS 连接。例如:

curl --ftp-ssl-ccc ftp://example.com

–ftp-ssl-control: 使用 SSL/TLS 控制连接。该选项可以在 FTP 连接中使用 SSL/TLS 控制连接。例如:

curl --ftp-ssl-control ftp://example.com

-y, --speed-limit: 设置下载速度限制。该选项可以设置下载速度限制。例如:

curl -y 100 -o file.zip http://example.com/file.zip

-Y, --speed-time: 设置下载速度限制时间。该选项可以设置下载速度限制时间。例如:

curl -Y 10 -y 100 -o file.zip http://example.com/file.zip

这些选项只是 curl 命令行中一部分,还有很多其他选项和功能可用。您可以使用 curl --help 命令来查看 curl 的完整选项列表,也可以参考 curl 的官方文档以获取更多信息