首页·终端命令

curl

网络

用法

curl [options] <url>

取回一个网址并把正文打印到终端;和浏览器不同,不加 `-L` 它不会跟随跳转,所以遇到 301 看起来像是空响应。

常用选项

选项含义
-LFollow redirects. curl does not follow them on its own, unlike a browser.
-iPrint the response headers along with the body; -I sends HEAD and prints headers only.
-d '{"a":1}'Send a request body. This alone already means POST.
-H "Content-Type: application/json"Add a request header; repeat -H for more.
-X PUTOverride the method. Unnecessary with -d, which is already a POST.
-o <file> / -OSave the body to a file / save it under the remote filename.
-s / -sSNo progress meter / silent but still print errors.
-fExit nonzero on HTTP 4xx and 5xx instead of printing the error page.
-kSkip certificate verification. Debugging only, never in production.

示例

curl -fsSL https://example.com/install.sh -o install.sh

Download quietly, follow redirects, fail loudly on a 404.

curl -i -H "Content-Type: application/json" -d '{"name":"a"}' https://api.example.com/items

POST JSON and read the response headers.

curl -s -o /dev/null -w "%{http_code}\n" https://example.com

Print just the status code.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. curl 是做什么的?

取回一个网址并把正文打印到终端;和浏览器不同,不加 `-L` 它不会跟随跳转,所以遇到 301 看起来像是空响应。

Q. 怎么写?

curl [options] <url> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 9 个,完整列表在 man curl。这条命令属于网络。

相关命令

man 手册: man curl