使用curl --fail获取页面输出

问题描述 投票:0回答:6

不带参数调用curl,我会得到页面输出,即使http状态代码= 404:

$ curl http://www.google.com/linux
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/linux</code> was not found on this server.  <ins>That’s all we know.</ins>

$ echo $?
0

状态码为0。

使用

--fail
调用它不会显示输出:

$ curl --fail http://www.google.com/linux
curl: (22) The requested URL returned error: 404 Not Found

$ echo $?
22

现在状态码是22...

即使 http status = 404, 500(如第一次curl 执行),我也想获得输出,同时获得不同的系统错误(如第二次curl 执行中,$? = 22)。

可以用curl吗?如果没有,我怎样才能用另一个工具实现这一点(这个工具必须接受文件上传和发布数据!

wget
似乎不是替代方案......)

http curl http-post wget postfile
6个回答
38
投票

现在可以通过curl 实现这一点。从 7.76.0 版本开始你可以做

curl --fail-with-body ...

这正是OP所要求的:显示文档正文并以代码22退出。

参见 https://curl.se/docs/manpage.html#--fail-with-body


36
投票

首先,错误代码(或退出代码)的最大值是

255
。这是参考

此外,

--fail
将不允许您做您正在寻找的事情。但是,您可以使用其他方法(编写 shell 脚本)来处理该情况,但不确定它是否对您有效!

http_code=$(curl -s -o out.html -w '%{http_code}'  http://www.google.com/linux;)

if [[ $http_code -eq 200 ]]; then
    exit 0
fi

## decide which status you want to return for 404 or 500
exit  204

现在执行

$?
,您将从那里获得退出代码。

您将在

out.html
文件中找到响应 html。

您还可以将 url 作为命令行参数传递给脚本。 检查这里


15
投票

不幸的是,卷曲是不可能的。但你可以使用 wget 来做到这一点。

$ wget --content-on-error -qO- http://httpbin.org/status/418

    -=[ teapot ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`
$ echo $?
8

8
投票

感谢@timaschew,这是我基于纯awk的增强版本:

curl_fail_with_body() {
  curl -o - -w "\n%{http_code}\n" "$@" | awk '{l[NR] = $0} END {for (i=1; i<=NR-1; i++) print l[i]}; END{ if ($0<200||$0>299) exit $0 }'
}

# example usage
curl_fail_with_body -sS http://httpbin.org/status/418

说明

  • -o - -w "\n%{http_code}\n"
    - 打印到标准输出(实际上它通过管道传输到下一个命令),最后带有状态代码
  • {l[NR] = $0} END {for (i=1; i<=NR-1; i++) print l[i]}
    - 打印除最后一行之外的所有行
  • END{ if ($0<200||$0>299) exit $0 }
    - 如果
    last line != 2xx
  • ,将以非零代码退出

替代版本,如果你想在命令后输出错误代码:

END{ if ($0<200||$0>299) {print "The requested URL returned error: " $0; exit 1}


顺便说一句,curl 从 v7.76.0 开始支持

--fail-with-body
选项。
此选项允许您在不使用外部工具的情况下实现所需的行为。


7
投票

我找到了解决方案,因为 wget 不适合发送 multipart/form-data

curl -o - -w "\n%{http_code}\n" http://httpbin.org/status/418 | tee >(tail -n 1 | cmp <(echo 2xx) - ) | tee >(grep "char 2"; echo $? > status-code) && grep 0 status-code

说明

-o - -w "\n%{http_code}\n"
- 打印到标准输出(实际上它通过管道传输到下一个命令),最后带有状态代码
tee
- 输出将通过管道传输到下一个命令,并另外打印到 stdout
tail -n 1
- 从最后一行提取状态代码
cmp <(echo 2xx) -
比较状态代码,仅第一个字符
grep "char 2"
- 如果第一个字符需要为 2,否则失败

在 shell 脚本中,您还可以进行更好的比较(目前它只允许 2xx,因此像 300 这样的重定向将被作为错误处理,

cmp
上面如何使用它)


0
投票

这是我的解决方案 - 它使用

jq
并假设正文是 json

#  this code adds a statusCode field to the json it receives and then jq squeezes them together
# curl 7.76.0 will have curl --fail-with-body and thus eliminate all this
  local result
  result=$(
    curl -sL -w ' { "statusCode": %{http_code}} ' -X POST "${headers[@]}" "${endpoint}" \
      -d "${body}"  "$curl_opts" | jq -ren '[inputs] | add'
  )
#   always output the result
  echo "${result}"
#  jq -e will produce an error code if the expression result is false or null - thus resulting in a
# error return code from this function naturally. This is much preferred rather than assume/hardcode
# the existence of a error object in the body payload
  echo "${result}" | jq -re '.statusCode >= 200 and .statusCode < 300' > /dev/null
© www.soinside.com 2019 - 2024. All rights reserved.