工程实践

curl的学习笔记

工作中遇到curl的命令,总结一下

curl 是一个命令行工具,用来按网址向服务器发请求、传数据。它背后的引擎是 libcurl(可被别的软件调用的网络传输库)。

日常最常用的就是这几件事:用 GET 拿网页或接口返回的内容;把结果存成文件;看响应头;用 POST 发表单或 JSON;加自定义请求头;跟随重定向;设超时;出现问题时加 -v 看实际发了什么。

可以先跑一下curl --version,看看本机版本和是否支持 HTTPS。

curl是什么?

  • curl(命令行工具):你在终端里键入的那个 curl。它按 URL 把数据从服务器拿过来,或把数据发出去。
  • libcurl(库):命令行工具背后的引擎。许多软件、语言绑定(比如 PHP 里的 curl 扩展)其实调的是这个库,不一定是命令行本身。

curl 不只能访问网页。它还支持 HTTP、HTTPS、FTP、SFTP 等很多协议。(HTTP 是浏览器和网站说话的协议;HTTPS 是加了加密的 HTTP。)

URL(Uniform Resource Locator,统一资源定位符)就是常说的网址,比如 https://curl.se/。它告诉程序:用什么协议、去哪台主机、要哪个资源。正式规范里它更广的名字是 URI,日常仍说 URL 就可以。

常用用法

默认情况下,curl 把拿到的正文打印在屏幕上。要存文件、看头信息、发数据,就要加标志。

  1. 发一个GET请求:

    curl https://curl.se/  # 把这个资源给我
  2. 把结果存成文件

curl -o thatpage.html https://www.example.com/
curl -O https://www.example.com/index.html
#-o 自己起名;-O(大写 O)用网址里的远程文件名。用 -O 时,URL 里必须带文件名,否则会失败:
  1. 看响应头

响应头(response headers)是服务器在正文之前先回来的一组说明,比如状态码、内容类型、是否要跳转。默认隐藏,用 -i和正文一起显示;用 -I只要头部(会发 HEAD 请求):

curl -i https://example.com/
curl -I https://example.com/
  1. POST 表单或 JSON

    POST 是另一种 HTTP 方法,用来把数据送给服务器,比如提交表单。-d 发表单常见的 name=value 字符串;发 JSON 建议用 --json(curl 7.82.0 起提供,会自动带上合适的请求头):

    curl -d 'name=admin&shoesize=12' <https://example.com/>
    curl --json '{"tool": "curl"}' <https://example.com/>

    --json 只是帮你把数据发出去并标成 JSON,curl 不会检查 你写的是不是合法 JSON。

  2. 自定义请求头、跟随重定向、超时、进度

    curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
    curl -L <https://example.com/>
    curl --max-time 5.5 <https://example.com/>
    curl -# -O <https://example.com/index.html>

常用标志

短选项和长选项通常等价,例如 -L 就是 —location。

-o / —output、-O / —remote-name

把下载内容写入文件,而不是打印到屏幕上。

curl -o thatpage.html https://www.example.com/
curl -O https://www.example.com/index.html

-i / —include、-I / —head

-i 把响应头和正文一起输出;-I 只取头部,不下载正文。

curl -i https://example.com/
curl -I https://example.com/

-d / —data、—data-urlencode、—json

-d 发送 POST 数据,默认按表单编码发送。数据里有空格等特殊字符时,可以让 curl 帮你做 URL 编码,也就是把空格等字符转成网址能安全传输的写法。—json 是发送 JSON 的捷径。

curl -d 'name=admin&shoesize=12' https://example.com/
curl --data-urlencode "name=I am Daniel" https://www.example.com
curl --json '{"tool": "curl"}' https://example.com/
curl --json @payload.json https://example.com/

技巧:如果本机 curl 较旧、没有 —json,可以写成 -d ’{…}’ -H ‘Content-Type: application/json’。

-H / —header

添加或替换请求头,也就是你随请求一起发给服务器的说明信息。

curl -H "Accept: application/json" https://example.com/

-A / —user-agent

设置 User-Agent(用户代理)。这是一个请求头,用来告诉服务器“我是谁”。curl 默认会发送类似 curl/8.x.x 的字符串。有些站点会按它返回不同内容。

curl -A "MyScript/1.0" https://example.com/

-L / —location

跟随重定向。服务器若返回了 Location: 头并配上 3xx 状态码,意思是“去另一个地址取”。默认最多跟随 30 次,可用 —max-redirs 修改。

curl -L https://example.com/

-m / —max-time、—connect-timeout

为请求设置时间上限,避免一直卡住。curl 很多操作默认没有总超时。-m 限制整次传输的最长秒数;—connect-timeout 只限制连接阶段,包括查找域名、建立 TCP 连接、加密连接。超时一般会以退出码 28 结束。小数用点号,不要用逗号。

curl --max-time 5.5 https://example.com/
curl --connect-timeout 2.37 https://example.com/

-# / —progress-bar、-s / —silent

下载到文件时,curl 常会显示进度。-# 换成简单进度条;-s 安静模式,不显示进度和错误提示,脚本里常用。

curl -# -O https://example.com/index.html
curl -s -o page.html https://example.com/

-v / —verbose

输出 curl 和服务器之间互相发送的内容,包括请求头、响应头、连接过程等。要更详细可以用 —trace-ascii debugdump.txt。

curl -v https://example.com/

-u / —user

提供用户名和密码,用于 HTTP 身份验证等场景。默认常用 Basic 认证。

curl -u myname:password https://www.example.com/

密码写在命令行里可能被其他进程看到,也会留在历史记录里。应该走 HTTPS。只写 -u myname、不写密码时,curl 会提示你输入。

-b / —cookie、-c / —cookie-jar

Cookie 是服务器存在你这边的小记忆,用来识别会话。-b 发送 cookie;-c 把收到的 cookie 存成文件。

curl -b cookies.txt -c cookies.txt https://www.example.com/

-x / —proxy

让请求先走代理,也就是一台替你中转访问的中间服务器。

curl -x my-proxy:888 https://www.example.com/

-f / —fail

HTTP 返回 404、401 等时,curl 默认仍视为“传输成功”,因为它确实收到了响应。加 -f 后,4xx/5xx 会让 curl 以失败退出,适合写脚本。

curl -f https://example.com/this-page-may-not-exist

-X / —request(少用、要慎)

它只改请求里写的方法名,不一定会改 curl 其他行为。正常情况几乎不该用 -X:光写 URL 就是 GET,-d 就是 POST,-I 就是 HEAD。要发 DELETE 这种标准里本来没有专门短选项的方法时,才用它:

curl -X DELETE https://example.com/

不要写多余的 curl -X POST -d data …,有 -d 已经是 POST。跟随重定向时乱加 -X,还可能让行为变怪。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

curl -H "Content-Type: application/json" -d '{"tool": "curl"}' <https://example.com/>
curl -L <https://example.com/>
curl --max-time 5.5 <https://example.com/>
curl -# -O <https://example.com/index.html>

-L 很重要:curl 默认不跟随重定向。浏览器会自动跳,curl 不会,要你明确说一声。

常见报错

  • curl: (6) Could not resolve host

    域名(网站名字)解析不出 IP 地址。先检查:URL 有没有打错、网络通不通、DNS 是否正常。也要看壳里的引号有没有把地址割断。

  • curl: (7) Failed to connect

    域名可能解出来了,但 TCP 连不上。常见原因:服务器没开、端口写错、防火墙拦了、代理要配没配。

  • curl: (28) 超时

    连接或整次传输超过了你设的时间,或等太久。可以抽查网络,或把 --connect-timeout、--max-time 调大一点再试。

  • curl: (60) 证书校验失败(certificate verify failed)

    curl 默认会验证 HTTPS 证书。失败可能是:自签名证书、中间证书没发全、本机 CA 证书库不全、证书和域名不匹配。

    正确做法是补上可信的 CA 证书,例如 --cacert ca-bundle.pem,而不是把安全检查关掉。--insecure / -k 会跳过校验,官方强烈建议只用于短暂实验,不要用在生产环境。

  • curl: (1) SSL is disabled, https: not supported

    你这份 curl 编译时没带 HTTPS/TLS 支持。需要换一个带 TLS 的正式安装版。

  • 「浏览器能打开,curl 却不跟跳」

    最常见原因是没加 -L。另外,浏览器还会跟 HTML 里的刷新跳转、JavaScript 跳转,curl 不解析 这两种,要自己看页面里写的新地址。

  • 明明 404 了,命令还是成功

    这是正常现象。curl 把 HTTP 响应成功拿回就算传输成功,不管状态码是 200 还是 404。写脚本时加 -f,或用 -w '%{response_code}\n' 把状态码打出来自己判断。

  • 引号、&、空格把命令搞坏

    这是壳的规则,不是 curl 的 bug。URL 里有 &、查询参数、JSON 里有双引号时,请把整段用引号保护:

curl '<https://www.example.com/cgi-bin/query?text=yes&q=curl>'
curl --json '{"tool": "curl"}' <https://example.com/>

​ Linux/常见终端里,JSON 更宜单引号;Windows 命令提示符通常要用双引号。

相关网站

  1. curl 官网
  2. curl 手册(man page)
  3. curl 教程
  4. 用 curl 编写 HTTP 请求
  5. Everything curl(在线书,官网入口见 curl.se/book.html)
  6. curl 常见问题 FAQ
  7. TLS 证书校验说明
  8. libcurl 介绍
  9. GitHub:curl/curl
  10. MDN:HTTP 概览(中文)

评论 0

评论保存在本站服务器,无需登录。带「作者」标识的是站主回复,请友善交流。

    评论内容至少2个字、最多1000字,按纯文本展示,不支持HTML。