bigywater.tech
常见报错 · 59

curl 常见错误码 (6)(7)(28)(35)(52)(56)(60) 实测:404 不加 -f 退出码是 0,-k 只是跳过验证,--retry 默认不重试 52、56、7

curl 的退出码很多,脚本里的 curl: (52)、(56)、(35)、(60) 常常搜不到准确的原因。这篇用 curl 8.23.0 和本机上自己起的小服务,把最常见的十几个错误码逐个造出来:每个的原文长什么样、和相邻错误码怎么区分、-f 和 -k 到底做了什么,以及 --retry 实际会重试哪些。

0
服务端返回 404 且不加 -f 时 curl 的退出码:HTTP 错误不算 curl 错误
3 次 / 1 次
--retry 2 默认下,超时(28)与 HTTP 503 的连接次数 / 空回复(52)的连接次数
5 out of 100
服务端回了头和 5 字节后停住,--max-time 2 的报错里 “bytes received” 前的数字

连接阶段

实测 · 本机执行输出
  主机名解析不了(.invalid 域名)
      curl 退出码 6;用时 0.02 s;curl: (6) Could not resolve host: no-such-host.invalid
  端口没有进程监听
      curl 退出码 7;用时 0.02 s;curl: (7) Failed to connect to 127.0.0.1:<端口> after 0 ms: Could not connect to server
  服务端收了请求但一直不回,--max-time 2
      curl 退出码 28;用时 2.02 s;curl: (28) Operation timed out after 2001 milliseconds with 0 bytes received
  服务端回了头和 5 字节就停了,--max-time 2
      curl 退出码 28;用时 2.02 s;curl: (28) Operation timed out after 2001 milliseconds with 5 out of 100 bytes received
  同一个服务,改用 --speed-limit 1000 --speed-time 2(速度低于 1000 字节/秒持续 2 秒就放弃)
      curl 退出码 28;用时 2.02 s;curl: (28) Operation too slow. Less than 1000 bytes/sec transferred the last 2 seconds

6 解析失败、7 连不上、28 超时(看已收到多少字节)。

服务端行为

实测 · 本机执行输出
  服务端收到请求直接关连接(52)
      curl 退出码 52;用时 0.02 s;curl: (52) Empty reply from server
  发了一部分之后 RST(56)
      curl 退出码 56;用时 0.22 s;curl: (56) Recv failure: Connection reset by peer
  Content-Length 1000 却只发了 500 字节就正常关闭(18)
      curl 退出码 18;用时 0.02 s;curl: (18) end of response with 500 bytes missing
  服务端返回 404,不加 -f(不算错误)
      curl 退出码 0;用时 0.02 s;(无错误输出)
  服务端返回 404,加 -f(22)
      curl 退出码 22;用时 0.02 s;curl: (22) The requested URL returned error: 404
  服务端返回 404,加 --fail-with-body(22,body 仍会输出)
      curl 退出码 22;用时 0.02 s;curl: (22) The requested URL returned error: 404
  重定向到自己,加 -L(47,默认最多 50 次)
      curl 退出码 47;用时 0.03 s;curl: (47) Maximum (50) redirects followed
  重定向到自己,加 -L --max-redirs 3
      curl 退出码 47;用时 0.03 s;curl: (47) Maximum (3) redirects followed
  URL 格式不对(3)
      curl 退出码 3;用时 0.03 s;curl: (3) bad range in position 9: | http://[bad/ |         ^
  协议不支持(foo://,退出码 1)
      curl 退出码 1;用时 0.02 s;curl: (1) Protocol "foo" not supported
  下游 head 提前退出时 curl 的退出码 = 23 | curl: (23) Failure writing output to destination, passed 16384 returned 0(23 = 写输出失败)

52 空回复、56 被重置、18 传输不完整、22 HTTP 错误(要加 -f)、47 重定向太多、23 下游先退出。

TLS

实测 · 本机执行输出
  https 访问自签名证书(没有 --cacert)
      curl 退出码 60;用时 0.04 s;curl: (60) SSL certificate OpenSSL verify result: self-signed certificate (18) | More details here: https://curl.se/docs/sslcerts.html |  | curl failed to verify the legitimacy of the server and therefore could not | establish a s
  指定 --cacert ok.crt 之后
      curl 退出码 0;用时 0.02 s;(无错误输出)
  指定了 CA,但证书是给 other.test 的(主机名不符)
      curl 退出码 60;用时 0.02 s;curl: (60) SSL: no alternative certificate subject name matches target hostname 'localhost' | More details here: https://curl.se/docs/sslcerts.html |  | curl failed to verify the legitimacy of the server and therefore could not | 
  加 -k 跳过验证(不推荐)
      curl 退出码 0;用时 0.04 s;(无错误输出)
  https 访问一个只会说明文 HTTP 的端口(35)
      curl 退出码 35;用时 0.10 s;curl: (35) TLS connect error: error:0A00010B:SSL routines::wrong version number
  http 访问一个 TLS 端口
      curl 退出码 56;用时 0.06 s;curl: (56) Recv failure: Connection reset by peer

60 证书验证失败,35 握手失败;-k 只是跳过验证。

--retry

实测 · 本机执行输出
  连接被拒(端口没人监听):默认: 退出码 7,用时约 0 s;--retry-connrefused: 退出码 7,用时约 2 s;--retry-all-errors: 退出码 7,用时约 2 s(用时 0 s 没重试,每次重试前 --retry-delay 1 秒)
  服务端 503:默认: 退出码 0,服务端收到 3 次连接;--retry-connrefused: 退出码 0,服务端收到 3 次连接;--retry-all-errors: 退出码 0,服务端收到 3 次连接
  服务端 500:默认: 退出码 0,服务端收到 3 次连接;--retry-connrefused: 退出码 0,服务端收到 3 次连接;--retry-all-errors: 退出码 0,服务端收到 3 次连接
  服务端 404(不加 -f):默认: 退出码 0,服务端收到 1 次连接;--retry-connrefused: 退出码 0,服务端收到 1 次连接;--retry-all-errors: 退出码 0,服务端收到 1 次连接
  服务端 404 + -f:默认: 退出码 22,服务端收到 1 次连接;--retry-connrefused: 退出码 22,服务端收到 1 次连接;--retry-all-errors: 退出码 22,服务端收到 3 次连接
  超时(28),--max-time 1:默认: 退出码 28,服务端收到 3 次连接;--retry-connrefused: 退出码 28,服务端收到 3 次连接;--retry-all-errors: 退出码 28,服务端收到 3 次连接
  空回复(52):默认: 退出码 52,服务端收到 1 次连接;--retry-connrefused: 退出码 52,服务端收到 1 次连接;--retry-all-errors: 退出码 52,服务端收到 3 次连接
  连接被重置(56):默认: 退出码 56,服务端收到 1 次连接;--retry-connrefused: 退出码 56,服务端收到 1 次连接;--retry-all-errors: 退出码 56,服务端收到 3 次连接

默认只重试超时和 HTTP 500/503;连接被拒、52、56 需要额外参数。

分步演示

退出码
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64;报错文本都是真实复现时原样捕获的(命令行工具统一用 LC_ALL=C 取英文原文,路径换成 );所有服务都是本机上自己起的、只监听 127.0.0.1,不访问外网;计时用 time.perf_counter,数字每次运行会有小幅抖动。本篇用 Homebrew curl 8.23.0(OpenSSL 4.0.3),不是 macOS 自带的 /usr/bin/curl(8.7.1,SecureTransport,TLS 错误文字不同);服务端是 Python 现场起的小服务,证书由 openssl 命令行现场生成;所有请求都带 --noproxy "*"
    • 没有验证:macOS 自带 curl 8.7.1 的 TLS 错误文字(用 SecureTransport / LibreSSL,35 与 60 的原文不同);HTTP/2、HTTP/3 下的专有错误码(如 92、95);代理环境下的错误码(5、97 等);Windows 上的 schannel 错误;--retry-max-time 与 Retry-After 头的配合;FTP / SMTP 等其它协议的错误码;连接超时 --connect-timeout 到黑洞地址的 28(没有构造黑洞地址)
    京ICP备2026067502号-1京公网安备11010502063487号