cRUL 工具
Table of Contents
1 cURL
cURL 是一个利用 URL 语法在命令行下工作的文件传输工具,1997 年首次发行。它支持 文件上传和下载,所以是综合传输工具,但按传统,习惯称 cURL 为下载工具。 Mac 和 Linux 可以通过命令行直接安装,Windows 需要下载可执行文件安装。可以通过 Everything curl 来学习使用工具。
2 常见用法
2.1 获取网页内容
直接使用 curl 后面添加网站地址就可以将网站的内容抓取下来,curl 默认将输出打印
到标准输出。curl 会统计下载的速度,如果不需要的话可以使用 -s 来关闭统计。
~ $ curl http://www.example.com/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1270 100 1270 0 0 1695 0 --:--:-- --:--:-- --:--:-- 1695<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
........
~ $ curl -s http://www.example.com/
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
2.2 保存成本地文件
使用 -o 选项可以将下载的内容保存成相应的文件,并命名成给定的文件名。
curl -o example.html http://www.example.com/
使用 -O 选项将下载的内容保存成 url 最后给定的文件名,如下例子中将会把文件命
名成 index.html
curl -O www.haxx.se/index.html
2.3 下载 ftp 文件时添加用户名和密码
有时候下载 ftp 中的内容需要用户名和密码,可以使用 -u 选项添加用户名和密码。
curl -u name:passwd ftp://machine.domain:port/full/path/to/file
2.4 HTTP 和 SOCKS 代理
curl 还支持 HTTP 和 SOCKS 协议的代理。可以使用 -x 选项了设置代理服务器的地
址和端口号信息。
curl -x my-proxy:888 ftp://ftp.leachsite.com/README curl -u user:passwd -x my-proxy:888 http://www.get.this/
2.5 分片下载 (Range)
可以使用 -r 选项来分片下载文件。
# Get the first 100 bytes of a document curl -r 0-99 http://www.get.this/ # Get the last 500 bytes of a document curl -r -500 http://www.get.this/
2.6 FTP/SMB 协议上传文件
使用 curl 的 -T 选项来指定上传文件。
# upload all data on stdin to a specified server curl -T - ftp://ftp.upload.com/myfile # upload file with username and password curl -T uploadfile -u user:passwd ftp://ftp.upload.com/ # upload file to get append to the remote file curl -T localfile -a ftp://ftp.upload.com/remotefile
使用 smb 协议上传文件。
curl -T file.txt -u "domain\username:passwd" smb://server.example.com/share/
3 API 测试
3.1 处理 HTTP 方法
3.1.1 提交表单数据
curl -X POST -d "user=foobar&pass=12345&id=blablabla&ding=submit" \ http://httpbin.org/post
{
"args": {},
"data": "",
"files": {},
"form": {
"ding": "submit",
"id": "blablabla",
"pass": "12345",
"user": "foobar"
},
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Content-Length": "47",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-5e5119ee-feb1917ffa6caf18e822a611"
},
"json": null,
"origin": "221.220.164.126",
"url": "http://httpbin.org/post"
}
3.1.2 命令行中指定 JSON 字符串
在测试 API 时常常需要 POST 数据,使用 -X 可以指定 HTTP 方法, -d 选项可以
指定 POST 数据。
curl -X POST -d '{"name":"Rafael, Sagula", "phone":3320780}' \ -H "Content-Type: application/json" http://httpbin.org/post
{
"args": {},
"data": "{\"name\":\"Rafael, Sagula\", \"phone\":3320780}",
"files": {},
"form": {},
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Content-Length": "42",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-5e5119ee-a70e66d0b7a6d01f6e2d190d"
},
"json": {
"name": "Rafael, Sagula",
"phone": 3320780
},
"origin": "221.220.164.126",
"url": "http://httpbin.org/post"
}
3.1.3 命令行中指定 JSON 文件
使用 -d 选项的局限性是只能使用普通键值对方式的参数,如果需要 POST 文件内容,
可以使用 @<filename> 来指定
cat /tmp/input.json
curl -X POST -d@/tmp/input.json \ -H "Content-Type: application/json" http://httpbin.org/post
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Content-Length": "0",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-5e5119ef-45e8d39aff11ccc8a396fcba"
},
"json": null,
"origin": "221.220.164.126",
"url": "http://httpbin.org/post"
}
参数则需要使用 -F 选项。 -F 选项使用 @<filename>;type=<mime-type> 这种
方式来编码所要上传的文件。如果没有给定 mime-type 则 curl 根据文件后缀名来猜
测。如下命令中上传了三个文件。
curl -X POST -F "coolfiles=@fil1.gif;type=image/gif,fil2.txt,fil3.html" \ http://www.post.com/postit.cgi
上传文件并且添加其它字段的例子。
curl -X POST -F "file=@cooltext.txt" -F "yourname=Daniel" \ -F "filedescription=Cool text file with cool text inside" \ http://www.post.com/postit.cgi
3.2 添加 USER AGENT
使用 -A 选项知道 USER-AGENT。
curl -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' \ https://httpbin.org/headers
{
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
"X-Amzn-Trace-Id": "Root=1-5e5119f0-0fcd159a9913b8d2dedfe6c2"
}
}
3.3 处理返回头和 HTTP 状态码
使用 -i 选项可以参考网页的给出的返回头相关信息。
curl -i http://httpbin.org/get
HTTP/1.1 200 OK
Date: Sat, 22 Feb 2020 12:09:21 GMT
Content-Type: application/json
Content-Length: 303
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-5e5119f1-6e8f68b458f901187185fa48"
},
"origin": "221.220.164.126",
"url": "http://httpbin.org/get"
}
使用 -v 选项会详细输出请求过程中的信息。
~ $ curl -v http://httpbin.org/get
* Couldn't find host httpbin.org in the .netrc file; using defaults
* Trying 34.230.136.58...
* TCP_NODELAY set
* Connected to httpbin.org (34.230.136.58) port 80 (#0)
> GET /get HTTP/1.1
> Host: httpbin.org
> User-Agent: curl/7.54.0
> Accept: application/json, application/xml, text/plain, */*
>
< HTTP/1.1 200 OK
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Date: Mon, 08 Jul 2019 15:37:21 GMT
< Referrer-Policy: no-referrer-when-downgrade
< Server: nginx
< X-Content-Type-Options: nosniff
< X-Frame-Options: DENY
< X-XSS-Protection: 1; mode=block
< Content-Length: 249
< Connection: keep-alive
<
{
"args": {},
"headers": {
"Accept": "application/json, application/xml, text/plain, */*",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0"
},
"origin": "223.72.66.144, 223.72.66.144",
"url": "https://httpbin.org/get"
}
* Connection #0 to host httpbin.org left intact
可以使用下面命令来将 header 下载起来保存成文件。
curl --dump-header headers.txt www.example.com
有时候我们在写脚本是仅仅需要参考网站的返回码,为了方便解析可以使用下面命令直 接获取返回码。
curl -q -s -w %{http_code} http://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-5e5119f1-fb363e300e5ada50e2ba2a08"
},
"origin": "221.220.164.126",
"url": "http://httpbin.org/get"
}
200
3.4 添加 Cookie
由于 HTTP 协议是无状态的,所以有些网站是使用 cookie 来记录会话信息。对于
chrome 这样的浏览器,可以轻易处理 cookie 信息,但在 curl 中只要增加相关参数也
是可以很容易的处理 cookie 。如下, -c 选项可以将获取到的 cookie 保存成文件。
curl -c cookie.txt http://httpbin.org/cookies
{
"cookies": {}
}
使用 -b 可以指定 cookie 字段。
curl -b "name=Daniel" http://httpbin.org/cookies
{
"cookies": {
"name": "Daniel"
}
}
读写同一个 cookie 文件。
curl -b cookies.txt -c cookies.txt www.example.com