zoukankan      html  css  js  c++  java
  • curl命令学习

    本文参考:
    阮一峰:http://www.ruanyifeng.com/blog/2019/09/curl-reference.html
    官方文档:https://catonmat.net/cookbooks/curl/debug-curl-requests

    curl

    • curl是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。curl还包含了用于程序开发的libcurl。
    • 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。

    简介

    • curl是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。curl还包含了用于程序开发的libcurl。
    • curl支持的通信协议有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP、Gopher、SCP、Telnet、DICT、FILE、LDAP、LDAPS、IMAP、POP3、SMTP和RTSP。
    • curl还支持SSL认证、HTTP POST、HTTP PUT、FTP上传, HTTP form based upload、proxies、HTTP/2、cookies、用户名+密码认证(Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos)、file transfer resume、proxy tunneling。

    curl常用参数

    [root@elk1 ~]# curl --help
    Usage: curl [options...] <url>
    Options: (H) means HTTP/HTTPS only, (F) means FTP only
         --anyauth       Pick "any" authentication method (H)
     -a, --append        Append to target file when uploading (F/SFTP)
         --basic         Use HTTP Basic Authentication (H)
         --cacert FILE   CA certificate to verify peer against (SSL)
         --capath DIR    CA directory to verify peer against (SSL)
     -E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
         --cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
         --ciphers LIST  SSL ciphers to use (SSL)
         --compressed    Request compressed response (using deflate or gzip)
     -K, --config FILE   Specify which config file to read
         --connect-timeout SECONDS  Maximum time allowed for connection
     -C, --continue-at OFFSET  Resumed transfer offset
     -b, --cookie STRING/FILE  String or file to read cookies from (H)
     -c, --cookie-jar FILE  Write cookies to this file after operation (H)
         --create-dirs   Create necessary local directory hierarchy
         --crlf          Convert LF to CRLF in upload
         --crlfile FILE  Get a CRL list in PEM format from the given file
     -d, --data DATA     HTTP POST data (H)
         --data-ascii DATA  HTTP POST ASCII data (H)
         --data-binary DATA  HTTP POST binary data (H)
         --data-urlencode DATA  HTTP POST data url encoded (H)
         --delegation STRING GSS-API delegation permission
         --digest        Use HTTP Digest Authentication (H)
         --disable-eprt  Inhibit using EPRT or LPRT (F)
         --disable-epsv  Inhibit using EPSV (F)
     -D, --dump-header FILE  Write the headers to this file
         --egd-file FILE  EGD socket path for random data (SSL)
         --engine ENGINGE  Crypto engine (SSL). "--engine list" for list
     -f, --fail          Fail silently (no output at all) on HTTP errors (H)
     -F, --form CONTENT  Specify HTTP multipart POST data (H)
         --form-string STRING  Specify HTTP multipart POST data (H)
         --ftp-account DATA  Account data string (F)
         --ftp-alternative-to-user COMMAND  String to replace "USER [name]" (F)
         --ftp-create-dirs  Create the remote dirs if not present (F)
         --ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
         --ftp-pasv      Use PASV/EPSV instead of PORT (F)
     -P, --ftp-port ADR  Use PORT with given address instead of PASV (F)
         --ftp-skip-pasv-ip Skip the IP address for PASV (F)
         --ftp-pret      Send PRET before PASV (for drftpd) (F)
         --ftp-ssl-ccc   Send CCC after authenticating (F)
         --ftp-ssl-ccc-mode ACTIVE/PASSIVE  Set CCC mode (F)
         --ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
     -G, --get           Send the -d data with a HTTP GET (H)
     -g, --globoff       Disable URL sequences and ranges using {} and []
     -H, --header LINE   Custom header to pass to server (H)
     -I, --head          Show document info only
     -h, --help          This help text
         --hostpubmd5 MD5  Hex encoded MD5 string of the host public key. (SSH)
     -0, --http1.0       Use HTTP 1.0 (H)
         --ignore-content-length  Ignore the HTTP Content-Length header
     -i, --include       Include protocol headers in the output (H/F)
     -k, --insecure      Allow connections to SSL sites without certs (H)
         --interface INTERFACE  Specify network interface/address to use
     -4, --ipv4          Resolve name to IPv4 address
     -6, --ipv6          Resolve name to IPv6 address
     -j, --junk-session-cookies Ignore session cookies read from file (H)
         --keepalive-time SECONDS  Interval between keepalive probes
         --key KEY       Private key file name (SSL/SSH)
         --key-type TYPE Private key file type (DER/PEM/ENG) (SSL)
         --krb LEVEL     Enable Kerberos with specified security level (F)
         --libcurl FILE  Dump libcurl equivalent code of this command line
         --limit-rate RATE  Limit transfer speed to this rate
     -l, --list-only     List only names of an FTP directory (F)
         --local-port RANGE  Force use of these local port numbers
     -L, --location      Follow redirects (H)
         --location-trusted like --location and send auth to other hosts (H)
     -M, --manual        Display the full manual
         --mail-from FROM  Mail from this address
         --mail-rcpt TO  Mail to this receiver(s)
         --mail-auth AUTH  Originator address of the original email
         --max-filesize BYTES  Maximum file size to download (H/F)
         --max-redirs NUM  Maximum number of redirects allowed (H)
     -m, --max-time SECONDS  Maximum time allowed for the transfer
         --metalink      Process given URLs as metalink XML file
         --negotiate     Use HTTP Negotiate Authentication (H)
     -n, --netrc         Must read .netrc for user name and password
         --netrc-optional Use either .netrc or URL; overrides -n
         --netrc-file FILE  Set up the netrc filename to use
     -N, --no-buffer     Disable buffering of the output stream
         --no-keepalive  Disable keepalive use on the connection
         --no-sessionid  Disable SSL session-ID reusing (SSL)
         --noproxy       List of hosts which do not use proxy
         --ntlm          Use HTTP NTLM authentication (H)
     -o, --output FILE   Write output to <file> instead of stdout
         --pass PASS     Pass phrase for the private key (SSL/SSH)
         --post301       Do not switch to GET after following a 301 redirect (H)
         --post302       Do not switch to GET after following a 302 redirect (H)
         --post303       Do not switch to GET after following a 303 redirect (H)
     -#, --progress-bar  Display transfer progress as a progress bar
         --proto PROTOCOLS  Enable/disable specified protocols
         --proto-redir PROTOCOLS  Enable/disable specified protocols on redirect
     -x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
         --proxy-anyauth Pick "any" proxy authentication method (H)
         --proxy-basic   Use Basic authentication on the proxy (H)
         --proxy-digest  Use Digest authentication on the proxy (H)
         --proxy-negotiate Use Negotiate authentication on the proxy (H)
         --proxy-ntlm    Use NTLM authentication on the proxy (H)
     -U, --proxy-user USER[:PASSWORD]  Proxy user and password
         --proxy1.0 HOST[:PORT]  Use HTTP/1.0 proxy on given port
     -p, --proxytunnel   Operate through a HTTP proxy tunnel (using CONNECT)
         --pubkey KEY    Public key file name (SSH)
     -Q, --quote CMD     Send command(s) to server before transfer (F/SFTP)
         --random-file FILE  File for reading random data from (SSL)
     -r, --range RANGE   Retrieve only the bytes within a range
         --raw           Do HTTP "raw", without any transfer decoding (H)
     -e, --referer       Referer URL (H)
     -J, --remote-header-name Use the header-provided filename (H)
     -O, --remote-name   Write output to a file named as the remote file
         --remote-name-all Use the remote file name for all URLs
     -R, --remote-time   Set the remote file's time on the local output
     -X, --request COMMAND  Specify request command to use
         --resolve HOST:PORT:ADDRESS  Force resolve of HOST:PORT to ADDRESS
         --retry NUM   Retry request NUM times if transient problems occur
         --retry-delay SECONDS When retrying, wait this many seconds between each
         --retry-max-time SECONDS  Retry only within this period
     -S, --show-error    Show error. With -s, make curl show errors when they occur
     -s, --silent        Silent mode. Don't output anything
         --socks4 HOST[:PORT]  SOCKS4 proxy on given host + port
         --socks4a HOST[:PORT]  SOCKS4a proxy on given host + port
         --socks5 HOST[:PORT]  SOCKS5 proxy on given host + port
         --socks5-basic  Enable username/password auth for SOCKS5 proxies
         --socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
         --socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy
         --socks5-gssapi-service NAME  SOCKS5 proxy service name for gssapi
         --socks5-gssapi-nec  Compatibility with NEC SOCKS5 server
     -Y, --speed-limit RATE  Stop transfers below speed-limit for 'speed-time' secs
     -y, --speed-time SECONDS  Time for trig speed-limit abort. Defaults to 30
         --ssl           Try SSL/TLS (FTP, IMAP, POP3, SMTP)
         --ssl-reqd      Require SSL/TLS (FTP, IMAP, POP3, SMTP)
     -2, --sslv2         Use SSLv2 (SSL)
     -3, --sslv3         Use SSLv3 (SSL)
         --ssl-allow-beast Allow security flaw to improve interop (SSL)
         --stderr FILE   Where to redirect stderr. - means stdout
         --tcp-nodelay   Use the TCP_NODELAY option
     -t, --telnet-option OPT=VAL  Set telnet option
         --tftp-blksize VALUE  Set TFTP BLKSIZE option (must be >512)
     -z, --time-cond TIME  Transfer based on a time condition
     -1, --tlsv1         Use => TLSv1 (SSL)
         --tlsv1.0       Use TLSv1.0 (SSL)
         --tlsv1.1       Use TLSv1.1 (SSL)
         --tlsv1.2       Use TLSv1.2 (SSL)
         --trace FILE    Write a debug trace to the given file
         --trace-ascii FILE  Like --trace but without the hex output
         --trace-time    Add time stamps to trace/verbose output
         --tr-encoding   Request compressed transfer encoding (H)
     -T, --upload-file FILE  Transfer FILE to destination
         --url URL       URL to work with
     -B, --use-ascii     Use ASCII/text transfer
     -u, --user USER[:PASSWORD]  Server user and password
         --tlsuser USER  TLS username
         --tlspassword STRING TLS password
         --tlsauthtype STRING  TLS authentication type (default SRP)
         --unix-socket FILE    Connect through this UNIX domain socket
     -A, --user-agent STRING  User-Agent to send to server (H)
     -v, --verbose       Make the operation more talkative
     -V, --version       Show version number and quit
     -w, --write-out FORMAT  What to output after completion
         --xattr        Store metadata in extended file attributes
     -q                 If used as the first parameter disables .curlrc
    

    curl语法及选项

    curl(7.29.0)所支持的选项(options)参数如下:
    在以下选项中,(H) 表示仅适用 HTTP/HTTPS ,(F) 表示仅适用于 FTP

        --anyauth      选择 "any" 认证方法 (H)
    -a, --append        添加要上传的文件 (F/SFTP)
        --basic        使用HTTP基础认证(Basic Authentication)(H)
        --cacert FILE  CA 证书,用于每次请求认证 (SSL)
        --capath DIR    CA 证书目录 (SSL)
    -E, --cert CERT[:PASSWD] 客户端证书文件及密码 (SSL)
        --cert-type TYPE 证书文件类型 (DER/PEM/ENG) (SSL)
        --ciphers LIST  SSL 秘钥 (SSL)
        --compressed    请求压缩 (使用 deflate 或 gzip)
    -K, --config FILE  指定配置文件
        --connect-timeout SECONDS  连接超时设置
    -C, --continue-at OFFSET  断点续转
    -b, --cookie STRING/FILE  Cookies字符串或读取Cookies的文件位置 (H)
    -c, --cookie-jar FILE  操作结束后,要写入 Cookies 的文件位置 (H)
        --create-dirs  创建必要的本地目录层次结构
        --crlf          在上传时将 LF 转写为 CRLF
        --crlfile FILE  从指定的文件获得PEM格式CRL列表
    -d, --data DATA    HTTP POST 数据 (H)
        --data-ascii DATA  ASCII 编码 HTTP POST 数据 (H)
        --data-binary DATA  binary 编码 HTTP POST 数据 (H)
        --data-urlencode DATA  url 编码 HTTP POST 数据 (H)
        --delegation STRING GSS-API 委托权限
        --digest        使用数字身份验证 (H)
        --disable-eprt  禁止使用 EPRT 或 LPRT (F)
        --disable-epsv  禁止使用 EPSV (F)
    -D, --dump-header FILE  将头信息写入指定的文件
        --egd-file FILE  为随机数据设置EGD socket路径(SSL)
        --engine ENGINGE  加密引擎 (SSL). "--engine list" 指定列表
    -f, --fail          连接失败时不显示HTTP错误信息 (H)
    -F, --form CONTENT  模拟 HTTP 表单数据提交(multipart POST) (H)
        --form-string STRING  模拟 HTTP 表单数据提交 (H)
        --ftp-account DATA  帐户数据提交 (F)
        --ftp-alternative-to-user COMMAND  指定替换 "USER [name]" 的字符串 (F)
        --ftp-create-dirs  如果不存在则创建远程目录 (F)
        --ftp-method [MULTICWD/NOCWD/SINGLECWD] 控制 CWD (F)
        --ftp-pasv      使用 PASV/EPSV 替换 PORT (F)
    -P, --ftp-port ADR  使用指定 PORT 及地址替换 PASV (F)
        --ftp-skip-pasv-ip 跳过 PASV 的IP地址 (F)
        --ftp-pret      在 PASV 之前发送 PRET (drftpd) (F)
        --ftp-ssl-ccc  在认证之后发送 CCC (F)
        --ftp-ssl-ccc-mode ACTIVE/PASSIVE  设置 CCC 模式 (F)
        --ftp-ssl-control ftp 登录时需要 SSL/TLS (F)
    -G, --get          使用 HTTP GET 方法发送 -d 数据  (H)
    -g, --globoff      禁用的 URL 队列 及范围使用 {} 和 []
    -H, --header LINE  要发送到服务端的自定义请求头 (H)
    -I, --head          仅显示响应文档头
    -h, --help          显示帮助
    -0, --http1.0      使用 HTTP 1.0 (H)
        --ignore-content-length  忽略 HTTP Content-Length 头
    -i, --include      在输出中包含协议头 (H/F)
    -k, --insecure      允许连接到 SSL 站点,而不使用证书 (H)
        --interface INTERFACE  指定网络接口/地址
    -4, --ipv4          将域名解析为 IPv4 地址
    -6, --ipv6          将域名解析为 IPv6 地址
    -j, --junk-session-cookies 读取文件中但忽略会话cookie (H)
        --keepalive-time SECONDS  keepalive 包间隔
        --key KEY      私钥文件名 (SSL/SSH)
        --key-type TYPE 私钥文件类型 (DER/PEM/ENG) (SSL)
        --krb LEVEL    启用指定安全级别的 Kerberos (F)
        --libcurl FILE  命令的libcurl等价代码
        --limit-rate RATE  限制传输速度
    -l, --list-only    只列出FTP目录的名称 (F)
        --local-port RANGE  强制使用的本地端口号
    -L, --location      跟踪重定向 (H)
        --location-trusted 类似 --location 并发送验证信息到其它主机 (H)
    -M, --manual        显示全手动
        --mail-from FROM  从这个地址发送邮件
        --mail-rcpt TO  发送邮件到这个接收人(s)
        --mail-auth AUTH  原始电子邮件的起始地址
        --max-filesize BYTES  下载的最大文件大小 (H/F)
        --max-redirs NUM  最大重定向数 (H)
    -m, --max-time SECONDS  允许的最多传输时间
        --metalink      处理指定的URL上的XML文件
        --negotiate    使用 HTTP Negotiate 认证 (H)
    -n, --netrc        必须从 .netrc 文件读取用户名和密码
        --netrc-optional 使用 .netrc 或 URL; 将重写 -n 参数
        --netrc-file FILE  设置要使用的 netrc 文件名
    -N, --no-buffer    禁用输出流的缓存
        --no-keepalive  禁用 connection 的 keepalive
        --no-sessionid  禁止重复使用 SSL session-ID (SSL)
        --noproxy      不使用代理的主机列表
        --ntlm          使用 HTTP NTLM 认证 (H)
    -o, --output FILE  将输出写入文件,而非 stdout
        --pass PASS    传递给私钥的短语 (SSL/SSH)
        --post301      在 301 重定向后不要切换为 GET 请求 (H)
        --post302      在 302 重定向后不要切换为 GET 请求 (H)
        --post303      在 303 重定向后不要切换为 GET 请求 (H)
    -#, --progress-bar  以进度条显示传输进度
        --proto PROTOCOLS  启用/禁用 指定的协议
        --proto-redir PROTOCOLS  在重定向上 启用/禁用 指定的协议
    -x, --proxy [PROTOCOL://]HOST[:PORT] 在指定的端口上使用代理
        --proxy-anyauth 在代理上使用 "any" 认证方法 (H)
        --proxy-basic  在代理上使用 Basic 认证  (H)
        --proxy-digest  在代理上使用 Digest 认证 (H)
        --proxy-negotiate 在代理上使用 Negotiate 认证 (H)
        --proxy-ntlm    在代理上使用 NTLM 认证 (H)
    -U, --proxy-user USER[:PASSWORD]  代理用户名及密码
        --proxy1.0 HOST[:PORT]  在指定的端口上使用 HTTP/1.0 代理
    -p, --proxytunnel  使用HTTP代理 (用于 CONNECT)
        --pubkey KEY    公钥文件名 (SSH)
    -Q, --quote CMD    在传输开始前向服务器发送命令 (F/SFTP)
        --random-file FILE  读取随机数据的文件 (SSL)
    -r, --range RANGE  仅检索范围内的字节
        --raw          使用原始HTTP传输,而不使用编码 (H)
    -e, --referer      Referer URL (H)
    -J, --remote-header-name 从远程文件读取头信息 (H)
    -O, --remote-name  将输出写入远程文件
        --remote-name-all 使用所有URL的远程文件名
    -R, --remote-time  将远程文件的时间设置在本地输出上
    -X, --request COMMAND  使用指定的请求命令
        --resolve HOST:PORT:ADDRESS  将 HOST:PORT 强制解析到 ADDRESS
        --retry NUM  出现问题时的重试次数
        --retry-delay SECONDS 重试时的延时时长
        --retry-max-time SECONDS  仅在指定时间段内重试
    -S, --show-error    显示错误. 在选项 -s 中,当 curl 出现错误时将显示
    -s, --silent        Silent模式。不输出任务内容
        --socks4 HOST[:PORT]  在指定的 host + port 上使用 SOCKS4 代理
        --socks4a HOST[:PORT]  在指定的 host + port 上使用 SOCKSa 代理
        --socks5 HOST[:PORT]  在指定的 host + port 上使用 SOCKS5 代理
        --socks5-hostname HOST[:PORT] SOCKS5 代理,指定用户名、密码
        --socks5-gssapi-service NAME  为gssapi使用SOCKS5代理服务名称
        --socks5-gssapi-nec  与NEC Socks5服务器兼容
    -Y, --speed-limit RATE  在指定限速时间之后停止传输
    -y, --speed-time SECONDS  指定时间之后触发限速. 默认 30
        --ssl          尝试 SSL/TLS (FTP, IMAP, POP3, SMTP)
        --ssl-reqd      需要 SSL/TLS (FTP, IMAP, POP3, SMTP)
    -2, --sslv2        使用 SSLv2 (SSL)
    -3, --sslv3        使用 SSLv3 (SSL)
        --ssl-allow-beast 允许的安全漏洞,提高互操作性(SSL)
        --stderr FILE  重定向 stderr 的文件位置. - means stdout
        --tcp-nodelay  使用 TCP_NODELAY 选项
    -t, --telnet-option OPT=VAL  设置 telnet 选项
        --tftp-blksize VALUE  设备 TFTP BLKSIZE 选项 (必须 >512)
    -z, --time-cond TIME  基于时间条件的传输
    -1, --tlsv1        使用 => TLSv1 (SSL)
        --tlsv1.0      使用 TLSv1.0 (SSL)
        --tlsv1.1      使用 TLSv1.1 (SSL)
        --tlsv1.2      使用 TLSv1.2 (SSL)
        --trace FILE    将 debug 信息写入指定的文件
        --trace-ascii FILE  类似 --trace 但使用16进度输出
        --trace-time    向 trace/verbose 输出添加时间戳
        --tr-encoding  请求压缩传输编码 (H)
    -T, --upload-file FILE  将文件传输(上传)到指定位置
        --url URL      指定所使用的 URL
    -B, --use-ascii    使用 ASCII/text 传输
    -u, --user USER[:PASSWORD]  指定服务器认证用户名、密码
        --tlsuser USER  TLS 用户名
        --tlspassword STRING TLS 密码
        --tlsauthtype STRING  TLS 认证类型 (默认 SRP)
        --unix-socket FILE    通过这个 UNIX socket 域连接
    -A, --user-agent STRING  要发送到服务器的 User-Agent (H)
    -v, --verbose      显示详细操作信息
    -V, --version      显示版本号并退出
    -w, --write-out FORMAT  完成后输出什么
        --xattr        将元数据存储在扩展文件属性中
    -q                .curlrc 如果作为第一个参数无效
    

    常用参数分类

    # 调试类
    -v, --verbose                          输出信息
    -q, --disable                          在第一个参数位置设置后 .curlrc 的设置直接失效,这个参数会影响到 -K, --config -A, --user-agent -e, --referer
    -K, --config FILE                      指定配置文件
    -L, --location                         跟踪重定向 (H)
    
    # CLI显示设置
    -s, --silent                           Silent模式。不输出任务内容
    -S, --show-error                       显示错误. 在选项 -s 中,当 curl 出现错误时将显示
    -f, --fail                             不显示 连接失败时HTTP错误信息
    -i, --include                          显示 response的header (H/F)
    -I, --head                             仅显示 响应文档头
    -l, --list-only                        只列出FTP目录的名称 (F)
    -#, --progress-bar                     以进度条 显示传输进度
    
    # 数据传输类
    -X, --request [GET|POST|PUT|DELETE|…]  使用指定的 http method 例如 -X POST
    -H, --header <header>                  设定 request里的header 例如 -H "Content-Type: application/json"
    -e, --referer                          设定 referer (H)
    -d, --data <data>                      设定 http body 默认使用 content-type application/x-www-form-urlencoded (H)
        --data-raw <data>                  ASCII 编码 HTTP POST 数据 (H)
        --data-binary <data>               binary 编码 HTTP POST 数据 (H)
        --data-urlencode <data>            url 编码 HTTP POST 数据 (H)
    -G, --get                              使用 HTTP GET 方法发送 -d 数据 (H)
    -F, --form <name=string>               模拟 HTTP 表单数据提交 multipart POST (H)
        --form-string <name=string>        模拟 HTTP 表单数据提交 (H)
    -u, --user <user:password>             使用帐户,密码 例如 admin:password
    -b, --cookie <data>                    cookie 文件 (H)
    -j, --junk-session-cookies             读取文件中但忽略会话cookie (H)
    -A, --user-agent                       user-agent设置 (H)
    
    # 传输设置
    -C, --continue-at OFFSET               断点续转
    -x, --proxy [PROTOCOL://]HOST[:PORT]   在指定的端口上使用代理
    -U, --proxy-user USER[:PASSWORD]       代理用户名及密码
    
    # 文件操作
    -T, --upload-file <file>               上传文件
    -a, --append                           添加要上传的文件 (F/SFTP)
    
    # 输出设置
    -o, --output <file>                    将输出写入文件,而非 stdout
    -O, --remote-name                      将输出写入远程文件
    -D, --dump-header <file>               将头信息写入指定的文件
    -c, --cookie-jar <file>                操作结束后,要写入 Cookies 的文件位置
    

    常用参数实例

    不带有任何参数时,curl 就是发出 GET 请求。

    curl https://www.baidu.com
    

    上面命令向www.baidu.com发出 GET 请求,服务器返回的内容会在命令行输出。

    -A

    -A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]。

    curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://www.baidu.com
    

    上面命令将User-Agent改成 Chrome 浏览器。

    curl -A '' https://www.baidu.com
    

    上面命令会移除User-Agent标头。

    也可以通过-H参数直接指定标头,更改User-Agent。

    curl -H 'User-Agent:php/1.0' www.baidu.com
    

    -B

    -b参数用来向服务器发送 Cookie。

    curl -b 'foo=bar' https://www.baidu.com
    

    上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。

    curl -b "fool=bar;foo2=bar2" https://www.baidu.com
    

    上面命令发送两个 Cookie。

    curl -b cookies.txt https://www.baidu.com
    

    上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

    -c

    -c参数将服务器设置的 Cookie 写入一个文件。

    curl -c cookies.txt https://www.baidu.com
    

    上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。

    -d

    -d参数用于发送 POST 请求的数据体。

    curl -d'login=emma&password=123'-X POST https://google.com/login
    

    或者

    curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
    

    使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。

    -d参数可以读取本地文本文件的数据,向服务器发送。

    curl -d '@data.txt' https://google.com/login
    

    上面命令读取data.txt文件的内容,作为数据体向服务器发送。

    --data-urlencode

    --data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

    curl --data-urlencode 'comment=hello world' https://google.com/login
    

    上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

    -e

    -e参数用来设置 HTTP 的标头Referer,表示请求的来源。

    curl -e "https://www.baidu.com?q=example" https://www.baidu.com
    

    上面命令将Referer标头设为https://www.baidu.com?q=example。

    -H参数可以通过直接添加标头Referer,达到同样效果。

    curl -H "Referer:https://www.baidu.com?q=example" https://www.baidu.com
    

    -F

    -F参数用来向服务器上传二进制文件。

    curl -F "file=@photo.png" https://www.baidu.com
    

    上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

    -F参数可以指定 MIME 类型。

    curl -F "file=@photo.png;type=image/png" https://www.baidu.com
    

    上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。

    -F参数也可以指定文件名。

    curl -F "file=@photo.png;filename=me.png" https://www.baidu.com
    

    上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。

    -G

    -G参数用来构造 URL 的查询字符串。

    curl -G -d 'q=kitties' -d 'count=20' https://www.baidu.com
    

    上面命令会发出一个 GET 请求,实际请求的 URL 为https://www.baidu.com /search?q=kitties&count=20。如果省略--G,会发出一个 POST 请求。

    如果数据需要 URL 编码,可以结合--data--urlencode参数。

    curl -G --data-urlencode 'comment=hello world' https://www.baidu.com
    

    -H

    -H参数添加 HTTP 请求的标头。

    curl -H 'Accept-Language:en-US' https://www.baidu.com
    

    上面命令添加 HTTP 标头Accept-Language: en-US。

    curl -H 'Accept-Language:en-US' -H 'Secret-Message:zywzyw' https://www.baidu.com
    

    上面命令添加两个 HTTP 标头。

    curl -d '{"login":"emma","pass":"123"}' -H 'Content-Type: application/json' https://www.baidu.com
    

    上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。

    -i

    -i参数打印出服务器回应的 HTTP 标头。

    curl -i https://www.baidu.com
    
    

    上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

    -I

    -I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

    curl -I https://www.baidu.com
    

    上面命令输出服务器对 HEAD 请求的回应。
    --head参数等同于-I。

    curl --head https://www.baidu.com
    

    -k

    -k参数指定跳过 SSL 检测。

    curl -k https://www.baidu.com
    

    上面命令不会检查服务器的 SSL 证书是否正确。

    -L

    -L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

    curl -L -d 'tweet=hi' https://www.baidu.com/tweet
    

    --limit-rate

    --limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

    curl --limit-rate 200k https://www.baidu.com
    

    上面命令将带宽限制在每秒 200K 字节。

    -o

    -o参数将服务器的回应保存成文件,等同于wget命令。

    curl -o home.html www.baidu.com
    

    上面命令将www.baidu.com保存成home.html。

    -O

    -O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

    curl -O www.baidu.com/index.html
    

    输出到index.html
    上面命令将服务器回应保存成文件,文件名为index.html。

    -s

    -s参数将不输出错误和进度信息。

    curl -s https://www.baidu.com
    

    上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

    如果想让 curl 不产生任何输出,可以使用下面的命令。

    curl -s -o /dev/null https://www.baidu.com
    

    -S

    -S参数指定只输出错误信息,通常与-s一起使用。

    curl -s -o /dev/null https://www.baidu.com
    

    上面命令没有任何输出,除非发生错误。

    -u

    -u参数用来设置服务器认证的用户名和密码。

    curl -u 'bob:12345' https://www.baidu.com/login
    

    上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
    curl 能够识别 URL 里面的用户名和密码。

    curl https://bob:12345@www.baidu.com/login
    

    上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。

    curl -u 'bob' https://www.baidu.com/login
    

    上面命令只设置了用户名,执行后,curl 会提示用户输入密码。

    -v

    -v参数输出通信的整个过程,用于调试。

    curl -v https://www.baidu.com
    

    --trace参数也可以用于调试,还会输出原始的二进制数据。

    curl --trace - https://www.baidu.com
    

    -x

    -x参数指定 HTTP 请求的代理。

    curl -x socks5://james:cats@myproxy.com:8080 https://www.baidu.com
    

    上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。

    如果没有指定代理协议,默认为 HTTP。

    curl -x james:cats@myproxy.com:8080 https://www.baidu.com
    

    上面命令中,请求的代理使用 HTTP 协议。

    -X

    -X参数指定 HTTP 请求的方法。

    curl -X POST https://www.baidu.com
    

    上面命令对https://www.baidu.com发出 POST 请求。

    curl状态码

  • 相关阅读:
    Prometheus监控Mongodb副本集
    Mongodb的分片集群基本操作
    Prometheus修改数据保留时间
    Logstash修改UTC时间为东八区时间(北京时间)
    Prometheus 邮件告警自定义模板的触发时间为utc的解决方法
    MongoDB 分片片键如何选择
    MongoDB分片(Sharding)技术
    prometheus进程监控
    prometheus各种告警规则
    Alertmanager邮件告警
  • 原文地址:https://www.cnblogs.com/even160941/p/14282591.html
Copyright © 2011-2022 走看看