curl 的退出码很多,脚本里的 curl: (52)、(56)、(35)、(60) 常常搜不到准确的原因。这篇用 curl 8.23.0 和本机上自己起的小服务,把最常见的十几个错误码逐个造出来:每个的原文长什么样、和相邻错误码怎么区分、-f 和 -k 到底做了什么,以及 --retry 实际会重试哪些。
主机名解析不了(.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 seconds6 解析失败、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 下游先退出。
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 peer60 证书验证失败,35 握手失败;-k 只是跳过验证。
连接被拒(端口没人监听):默认: 退出码 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 需要额外参数。