Curl usage: HTTP request, file download, FTP upload and download

1. Introduction to curl command

CURL (CommandLine Uniform Resource Locator) is a network request tool used under the command line terminal using URL syntax. It supports HTTP, HTTPS, FTP and other protocols. CURL also has a version of libcurl for program development.

Linux and MAC generally have curl installed by default in the system. Just use the command directly in the terminal. If you need to install it manually, you can download and install it at curl.haxx.se.

Windows system curl download address: https://curl.haxx.se/windows/, you can use it after downloading and decompressing. The executable file of the command is in the decompressed bin folder.

2. Format of curl command

2.1 Basic grammar

curl [options...] <url>
1
cURL 文档主页: https://curl.haxx.se/docs/manpage.html
cURL 帮助指南: https://curl.haxx.se/docs/manual.html
2.2 常用参数
PS: (H) 表示只给 HTTP/HTTPS 使用, (F) 表示只给 FTP 使用

常用参数: Show Info
————————————————

-h/--help                       # 打印帮助信息
-V/--version                    # 显示版本信息
-s/--silent                     # 静默模式, 不输出任何内容
-i/--include                    # 输出包含 headers 信息
-v/--verbose                    # 输出详细内容
-#/--progress-bar               # 以进度条方式显示传输过程
————————————————

Common parameters: Headers

-H/--header     LINE        (H) # 添加请求头, 可添加多个 -H 参数, 
                                # 参数格式: -H "NAME: VALUE"

-A/--user-agen  STRING      (H) # 请求头的 User-Agent 字段
-e/--referer    URL         (H) # 请求头的 Referer 字段
-r/--range      RANGE       (H) # 请求头的 Range 字段
-b/--cookie     STRING/FILE (H) # 请求头的 Cookie 字段, 以字符串的形式提供, 
                                # 或从指定 cookie 文件中读取

-c/--cookie-jar     FILE    (H) # 把响应头中的 cookie 保存到指定文件

-D/--dump-header    FILE        # 把 headers 信息保存指定文件
-I/--head                       # 只显示文档信息(只显示响应头)

Common parameters: Request Content

# 执行命令, 如果是 HTTP 则是请求方法, 如: GET, POST, PUT, DELETE 等
#          如果是 FTP 则是执行 FTP协议命令
-X/--request    COMMAND

# HTTP POST 请求内容(并自动发出 POST 请求), 例如: aa=bb&cc=dd
-d/--data       DATA    (H)

# HTTP multipart POST 表单数据,(并自动发出 POST 请求)
# 多个表单字段可添加多个 -H 参数, 如果是文件参数, 路径值前面需要加@
# 参考格式: -F "name1=@/filepath" -F "name2=stringvalue"
-F/--form       CONTENT (H)                   

Common parameters: Response Content

-o/--output FILE    FILE        # 把响应内容输出到指定文件
-O/--remote-name                # 以 URL 的文件名作为文件名称保存响应内容到当前目录
-C/--continue-at    OFFSET      # 断点续传, 从 offset 位置继续传输

Common parameters: Other

-y/--speed-time     SECONDS     # 连接 超时时间, 单位: 秒,  默认为 30
-m/--max-time       SECONDS     # 读取 超时时间, 必须在该时间内传输完数据, 单位: 秒
--limit-rate        RATE        # 限速传输, 单位: Byte/s

-x/--proxy          [PROTOCOL://]HOST[:PORT]    # 设置代理
-U/--proxy-user     USER[:PASSWORD]             # 代理的用户名和密码
-u/--user           USER[:PASSWORD][;OPTIONS]   # 设置服务器的用户密码和登录选项
--cacert            FILE                  (SSL) # 使用指定的 CA 证书

-P/--ftp-port       ADR (F) # 指定 FTP 传输的端口
-T/--upload-file    FILE    # 上传文件到指定的 URL (http/ftp) 位置, 
                            # 参考格式: -T "file1" 或 -T "{file1,file2}"

-Q/--quote    CMD  (F/SFTP) # 执行命令, -X 只执行一条命令, -Q 可执行多条,
                            # 多条命令将按顺序执行,
                            # 参考格式: -Q "cmd1" -Q "cmd2"

3. Examples of using the curl command

3.1 HTTP/HTTPS network request

普通 GET 请求
curl https://www.baidu.com/         # GET请求, 输出 响应内容
curl -I https://www.baidu.com/      # GET请求, 只输出 响应头
curl -i https://www.baidu.com/      # GET请求, 输出 响应头、响应内容
curl -v https://www.baidu.com/      # GET请求, 输出 通讯过程、头部信息、响应内容等

POST request to submit data

# POST 提交 JSON 数据(\表示命令语句还未结束, 换行继续)
curl -H "Content-Type: application/json"                \
     -d '{"username":"hello", "password":"123456"}'     \
     http://localhost/login

# POST 提交 表单数据
curl -F "username=hello"                \
     -F "password=123456"               \
     -F "[email protected]"      \
     http://localhost/register

download file

curl https://www.baidu.com -o baidu.txt

# 使用 URL 指定的资源文件名保存下载文件(URL 必须指向具体的文件名)
curl https://www.baidu.com/index.html -O

# 指定 Usaer-Agent 和 Referer 请求头的值, 下载文件
curl -A "Mozilla/5.0 Chrome/70.0.3538.110 Safari/537.36" \
     -e "https://www.baidu.com/" \
     https://www.baidu.com/index.html -O

3.2 FTP upload/download files

假设 FTP 服务器 地址为:192.168.0.100; 用户名为:user; 密码为:passwd
(1) 查看文件
# 查看 FTP 指定目录(目录必须以"/"结尾)下的文件列表 
curl ftp://192.168.0.100/aaDir/ -u "user:passwd"

# 查看 FTP 指定文件的内容(直接输出到终端) 
curl ftp://192.168.0.100/aaDir/aa.txt -u "user:passwd"

# 用户名 和 密码 的另一种写法(查看 FTP 服务器指定目录)
curl ftp://user:[email protected]/aaDir/

(2) 上传文件
# 上传 aa.txt 文件到 FTP 指定目录下(目录必须以"/"结尾), 并以 原文件名 命名保存
curl ftp://192.168.0.100/aaDir/ -u "user:passwd" -T "aa.txt"

# 上传 aa.txt 文件到 FTP 指定目录下, 并以 bb.txt 命名保存
curl ftp://192.168.0.100/aaDir/bb.txt -u "user:passwd" -T "aa.txt"

# 同时上传多个文件
curl ftp://192.168.0.100/aaDir/ -u "user:passwd" -T "{aa.txt,bb.txt}"

(3) 下载文件
# 下载 FTP 指定文件 /aaDir/aa.txt, 以原文件名命名保存到当前目录 
curl ftp://192.168.0.100/aaDir/aa.txt -u "user:passwd" -O

# 下载 FTP 指定文件 /aaDir/aa.txt, 以 bb.txt 命名保存
curl ftp://192.168.0.100/aaDir/aa.txt -u "user:passwd" -o bb.txt

(4) 执行 FTP 协议命令
curl 执行 FTP 命令格式:
单条命令: curl [-options] <ftpUrl> -X "FTP命令"
多条命令: curl [-options] <ftpUrl> -Q "FTP命令" -Q "FTP命令"
# 
# 创建文件夹, 在 /aaDir/ 目录(目录必须以"/"结尾)下创建 bbDir 文件夹
#
curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "MKD bbDir"

# 
# 删除文件夹, 删除 /aaDir/ 目录下的 bbDir 文件夹(文件夹必须为空)
#
curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "RMD bbDir"

# 
# 删除文件, 删除 /aaDir/ 目录下的 aa.txt 文件
#
curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "DELE aa.txt"

#
# 重命名, 重命名需要连续执行两条命令, 使用两个 -Q 参数连续执行两条命令(必须先 RNFR, 后 RNTO)
#
curl -u "user:passwd" ftp://192.168.0.100/ -Q "RNFR OldPath" -Q "RNTO NewPath"

Guess you like

Origin blog.51cto.com/13528748/2677943