Curl 使用手册

简介

CURL (Client URL)是一个命令行工具,用于在命令行下发送 HTTP 请求。对于热爱命令行的人来说,这个是必备的工具。

基本用法

1
2
3
4
> curl https://api.github.com/versions
[
"2022-11-28"
]

常用参数

-A

设置请求头中的代理头 User-Agent 字段。curl 默认的代理头是 curl/[version]

例如机器内的 curl 版本是 7.64.1,那么默认的 User-Agent 就是 curl/7.64.1

1
"GET /versions HTTP/1.1" 200 250 "-" "curl/7.64.1"

如果我们想要自定义 User-Agent,可以使用 -A 参数,以下示例就将请求头改为了 Edge 浏览器的 User-Agent

1
> curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78" https://api.github.com/versions

也可以通过 -H 参数来设置请求头

1
> curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78" https://api.github.com/versions

-b

设置请求头中的 Cookie 字段

1
> curl -b "logged_in=yes;dotcom_user=stolenzc;" https://api.github.com/versions

如果需要读取文件,可以使用

1
> curl -b cookie.txt https://api.github.com/versions

-c

将服务器返回的响应头中的 Cookie 字段写入文件

1
> curl -c cookie.txt https://api.github.com/versions

-d

发送 POST 请求的请求体数据

1
2
3
> curl -X POST -d "name=stolenzc&password=123" https://api.github.com/versions
> curl -X POST -d "name=stolenzc" -d "password=123" https://api.github.com/versions
> curl -X POST -d '@data.json' https://api.github.com/versions

默认的请求头中的 Content-Type 是 application/x-www-form-urlencoded,如果需要发送 JSON 格式的数据,可以使用 -H 参数来设置请求头

1
> curl -X POST -H "Content-Type: application/json" -d '{"name":"stolenzc","password":"123"}' https://api.github.com/versions

–data-urlencode

发送 POST 请求的请求体数据,会对数据进行 URL 编码

1
> curl -X POST --data-urlencode "name=stolen zc&password=123" https://api.github.com/versions

-e

设置请求头中的 Referer 字段

1
> curl -e "https://stolenzc.com" https://api.github.com/versions

也可以通过 -H 参数来设置请求头中的改参数

1
> curl -H "Referer: https://stolenzc.com" https://api.github.com/versions

-F

发送 POST 请求的请求体数据,用于上传二进制文件

1
> curl -F 'file=@photo.png' https://api.github.com/versions

该参数会自动设置请求头中的 Content-Type 为 multipart/form-data

-F 也可以设置 MIME 类型

1
> curl -F 'file=@photo.png;type=image/png' https://api.github.com/versions

-G

用来构建 GET 请求的请求体数据

1
2
3
> curl -G -d "name=stolenzc" -d "password=123" https://api.github.com/versions

实际请求url为: https://api.github.com/versions?name=stolenzc&password=123

默认会发送一个 GET 请求,如果去掉 -G 参数,那么会发送一个 POST 请求,如果需要对数据进行 URL 编码,可以使用 --data-urlencode 参数

1
> curl -G --data-urlencode "name=stolen zc&password=123" https://api.github.com/versions

-H

设置 http 请求头

1
> curl -H "Content-Type: application/json" -d '{"name": "stolenzc", "password": "123"}' https://api.github.com/versions

-i

用于打印出服务器回应的 http 头信息

会先打印出响应头,然后空一行,然后打印出响应体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
> curl -i https://api.github.com/versions
HTTP/2 200
server: GitHub.com
date: Mon, 06 Feb 2023 14:33:56 GMT
content-type: application/json; charset=utf-8
content-length: 19
cache-control: public, max-age=60, s-maxage=60
vary: Accept, Accept-Encoding, Accept, X-Requested-With
etag: "e2ebdf109e4e2a25e3dafbe449a68700db7044ebea246b289a65139cb0a0b333"
x-github-media-type: github.v3; format=json
x-github-api-version-selected: 2022-11-28
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
access-control-allow-origin: *
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'
x-ratelimit-limit: 60
x-ratelimit-remaining: 55
x-ratelimit-reset: 1675694126
x-ratelimit-resource: core
x-ratelimit-used: 5
accept-ranges: bytes
x-github-request-id: 4DB5:16B1:542E0:5CB14:63E10FD4

[
"2022-11-28"
]

-I/–head

像服务器发送 HEAD 请求,只打印出响应头信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
> curl -I https://api.github.com/versions
HTTP/2 200
server: GitHub.com
date: Mon, 06 Feb 2023 14:37:00 GMT
content-type: application/json; charset=utf-8
content-length: 19
cache-control: public, max-age=60, s-maxage=60
vary: Accept, Accept-Encoding, Accept, X-Requested-With
etag: "e2ebdf109e4e2a25e3dafbe449a68700db7044ebea246b289a65139cb0a0b333"
x-github-media-type: github.v3; format=json
x-github-api-version-selected: 2022-11-28
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
access-control-allow-origin: *
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'
x-ratelimit-limit: 60
x-ratelimit-remaining: 58
x-ratelimit-reset: 1675697820
x-ratelimit-resource: core
x-ratelimit-used: 2
accept-ranges: bytes
x-github-request-id: 4D93:3C49:65424:6E189:63E1109A

-k

忽略 ssl 证书验证

1
> curl -k https://api.github.com/versions

-L

让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

1
> curl -L -d 'tweet=hi' https://api.twitter.com/tweet

limit-rate

限制传输速度,模拟慢速网络环境

1
curl --limit-rate 1000B https://api.github.com/versions

-o

将服务器响应的内容保存到文件中,类似于 wget 命令

1
> curl -o get_docker.sh https://get.docker.com

-O

将服务器响应的内容保存到文件中,文件名从 URL 中获取

1
> curl -O https://www.baidu.com/index.html

如果无法从 URL 中获取文件名,下载会失败

-s/–silent

静默模式,不输出任何错误信息,如果报错,输出为空,如果正常,输出正常的响应内容

1
> curl -s https://api.github.com/versions

-S

指定输出错误信息,通常与 -s 一起使用

1
> curl -sS https://api.github.com/versions

-u

指定用户名和密码,用于 HTTP 认证

1
> curl -u username:password https://api.github.com/versions

会自动设置 http 请求头中的 Authorization: Basic xxx,其中 xxx 是 base64 编码的用户名和密码

如果只输入了用户名,没有输入密码,会提示输入密码

1
2
> curl -u username https://api.github.com/versions
> password

-v/–trace

输出通信过程,用于调试

1
> curl -v https://api.github.com/versions

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

1
> curl --trace - https://api.github.com/versions

-x

指定代理服务器

1
> curl -x socks5://127.0.0.1:1080 https://api.github.com/versions

-X

指定 HTTP 请求方法

1
> curl -X POST -d 'username=stolenzc' https://api.github.com/versions

参考自阮一峰老师的网络日志:curl 命令详解,部分内容有删改